MVC 3 Layout and Controllers

I am creating MVC applications 3. The application should be able to display a different layout according to the subdomain (for example: customer1.mysite.com β†’ layout1; customer2.mysite.com β†’ layout2, etc.), It will also have a layout for mobile and IE 6

I saw that it is _ViewStart.cshtml that I can use logic to set the layout. But I don’t understand where is it for this? Do I have to write all the code in the view?

Another layout question is how to make you define code for general behavior? Do you have a controller for this?

And the last thing I saw the concept of areas in asp.net MVC2, now it is out of date, what do we have Razor?

thanks for the help

Fred

+3
source share
2 answers

_ViewStart, , , . , .

+1

, ViewBag.

, OnActionExecuting ViewBag. BaseController, , ActionFilter.

_ViewStart switch ViewBag .

, ActionFilter, @ViewBag.Subdomain Razor, _ViewStart.cshtml.

public class AddSubdomainToViewDataAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        var subdomain = filterContext.HttpContext.Request.Url.Authority.Split('.').First();
        var controller = filterContext.Controller as Controller;
        controller.ViewData.Add("Subdomain", subdomain);
    }
}

, [AddSubdomainToViewData].

, _ViewStart.cshtml - :

@{
    Layout = "~/Views/Shared/" + ((@ViewContext.ViewData["Subdomain"] as String) ?? String.Empty) + "_layout.cshtml";
}

Razor.

+2

All Articles