0

Mypage.cshtml have a page and have a domain "xxx.com" and a subdomain "test.xxx.com" that when I publish my site in the subdomain "test.xxx.com" the title of the page shows something different. It would be something like:

@if (--Request.IsSubDomain("test")--)
{ 
    <h1>Test page: Product</h1>
}
else
{ 
    <h1>Product</h1>
}
ridermansb
  • 10,779
  • 24
  • 115
  • 226

2 Answers2

1

You would not want to have this login in your views. I personally would move this into a custom attribute or include the logic directly in your controllers.

By the way, what is the question?

Edit:

You could use Request.Url.Authority to determine the domain.

Digbyswift
  • 10,310
  • 4
  • 38
  • 66
0

I don't normally recommend using ViewBag, but if you only want to use this to render a title, subclass your controllers from a parent controller say, GeneralController and set a ViewBag.Title property there based on domain.

One alternative to this is subclassing any view models from a base view model including similar logic.

public class GeneralController
{
    protected override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        base.OnActionExecuting(filterContext);

        if(HttpContext != null)
            ViewBag.Title = GetRequestPath();        
    }

    private string GetRequestTitle()
    {
        if(HttpContext.Request.Path.Contains("test.xxx"))
            return "Test site";
    }
}

Then, any controller and subsequently rendered views will be able to use this ViewBag.Title property. In fact, out of the box, MVC3 includes _Layout.cshtml as its default layout (or Master Page) that already contains the following line in the head:

<title>@ViewBag.Title</title>
David Fox
  • 10,603
  • 9
  • 50
  • 80