MVC 3 布局和控制器

发布于 2024-10-22 04:37:43 字数 345 浏览 1 评论 0原文

我正在构建 MVC 3 应用程序。应用程序应该能够根据子域名显示不同的布局(例如:customer1.mysite.com ->布局1;customer2.mysite.com ->layout2;等等...)它还有一个布局mobile 和 IE 6。

我已经看到它们是 _ViewStart.cshtml,我可以利用它来执行设置布局的逻辑。但我不明白的是控制器在哪里?我应该在视图中编写所有代码吗?

布局的另一个问题如何分解常见行为的代码?你有一个控制器吗?

最后我在 ASP.NET MVC2 中看到了区域的概念,既然我们有了 Razor,它就已经过时了吗?

谢谢你的帮助

弗雷德

I m building a MVC 3 applications. The application should be able to display a different layout according to the sub domaine (ex: customer1.mysite.com -> layout1; customer2.mysite.com -> layout2; etc...) it will have also a layout for mobile and IE 6.

I have seen that their is the _ViewStart.cshtml that I can leverage to do the logic to set the layout. But what I don't get is where is the controler for that? Should I write all the code in the view?

An other question with layout how to do you factor out the code for the common behaviours? Do you have a controler for that?

And a last one I have seen the concept of areas in asp.net MVC2 is it obsolete now that we have Razor?

Thank you for your help

Fred

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

欢烬 2024-10-29 04:37:43

这听起来是使用 ViewBag

这个想法是,在 OnActionExecuting 期间,您将查找子域并将其放入 ViewBag 中。这可以在其他控制器继承的自定义 BaseController 中完成,或者从 ActionFilter 完成

然后,在你的_ViewStart中,你可以在ViewBag上编写一个switch语句来控制布局。

例如,下面是一个 ActionFilter,它将填充任何 Razor 视图中的 @ViewBag.Subdomain,包括 _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 布局。

This sounds like a good time to use ViewBag.

The idea is that during OnActionExecuting, you would look up the subdomain and shove it into the ViewBag. This can be done in a custom BaseController from which your other controllers inherit, or from an ActionFilter.

Then, in your _ViewStart, you can write a switch statement on ViewBag to control layout.

For example, here is an ActionFilter that will populate @ViewBag.Subdomain in any of your Razor views, including _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);
    }
}

Then, decorate your controllers with this new [AddSubdomainToViewData] attribute.

Finally, in _ViewStart.cshtml, do something like this:

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

This will use a different Razor layout for each subdomain.

绝影如岚 2024-10-29 04:37:43

While you could do this in the _ViewStart I think that a better way would be to write a custom view engine in which based on the user agent or the domain include a different layout. Then you would have common controllers and views, only the layout will differ.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文