MVC 3 布局和控制器
我正在构建 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这听起来是使用 ViewBag。
这个想法是,在 OnActionExecuting 期间,您将查找子域并将其放入 ViewBag 中。这可以在其他控制器继承的自定义 BaseController 中完成,或者从 ActionFilter 完成。
然后,在你的_ViewStart中,你可以在ViewBag上编写一个switch语句来控制布局。
例如,下面是一个 ActionFilter,它将填充任何 Razor 视图中的
@ViewBag.Subdomain
,包括 _ViewStart.cshtml。然后,使用这个新的
[AddSubdomainToViewData]
属性来装饰您的控制器。最后,在 _ViewStart.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.Then, decorate your controllers with this new
[AddSubdomainToViewData]
attribute.Finally, in _ViewStart.cshtml, do something like this:
This will use a different Razor layout for each subdomain.
虽然您可以在 _ViewStart 中执行此操作,但我认为更好的方法是编写 自定义视图引擎< /a> 其中基于用户代理或域包括不同的布局。然后您将拥有通用的控制器和视图,只是布局会有所不同。
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.