0

How do I use a wildcard in the URL of my comparison for Request.URL.Host? I have a site that's been running for years with the test site showing as blue. The comparison used to use the entire URL.

    @if (Request.Url.Host == "URLtest.site.com") //
    {
    bodyclass = "test";
    }

We are adding a second test site and I'd like to set this one time. So I would like to use a wildcard. Something like this, but it doesn't seem to work. I'm sure I'm missing something simple.

@if (Request.Url.Host == "*test.*") //
{
    bodyclass = "test";
}
1
  • 1
    I believe you should use a regex for that comparison. Commented Sep 30, 2020 at 23:08

2 Answers 2

1

You can use RegEx as suggested in other answers.

A simpler check would be to use string.Contains()

@if (Request.Url.Host.Contains("test.")) //
{
    bodyclass = "test";
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. I started down this path but wasn't sure how to set the if statement using contains. This is exactly what I was looking for.
1

Please use Regex Match method

using System.Text.RegularExpressions;


var pattern = @"\w*test.\w*";
string input = "url2test.site.com";
var match = Regex.Match(input, pattern, RegexOptions.IgnoreCase);
if (match .Success)
{
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.