如何在MVC3 razor视图下的ViewsStart文件中设置使用两套不同的Layout布局视图

http://stackoverflow.com/questions/5161380/how-do-i-specify-different-layouts-in-the-asp-net-mvc-3-razor-viewstart-file

问题描述: 如何在同一应用程序中应用两套布局视图,两个不同控制器对应的使用两套不同的视图,如PublicControlller对应_PublicLayout而StuffController对应_StuffLayout,

如何通过在_ViewStart文件中进行相关设置使得View/Public文件夹下的文件默认使用的_PublicLayout布局页,而View/Stuff文件夹下的文件默认使用的_StuffLayout布局页。

 

解决方案:

1)可以在各自文件夹中放置新的_ViewStart.cshtml文件改写View文件夹下的_ViewStart并制定使用特定的_Layout布局页。如View/Public和View/stuff文件夹下各自有

自己的_ViewStart文件设置布局页面。

2)在controller下的action方法中指定使用某个布局页面,如 

returnView("Index","~/Views/Shared/_StaffLayout.cshtml", someViewModel);
3)自定义一个AcitonFilter类在调用执行action时来改写使用的布局页

publicclassLayoutInjecterAttribute:ActionFilterAttribute
{
   
privatereadonlystring _masterName;
   
publicLayoutInjecterAttribute(string masterName)
   
{
        _masterName
= masterName;
   
}

   
publicoverridevoidOnActionExecuted(ActionExecutedContext filterContext)
   
{
       
base.OnActionExecuted(filterContext);
       
var result = filterContext.ResultasViewResult;
       
if(result !=null)
       
{
            result
.MasterName= _masterName;
       
}
   
}
}

通过在controller下的action方法添加特性来指定选择某特定布局页

[LayoutInjecter("_PublicLayout")]
publicActionResultIndex()
{
   
returnView();}

}

http://forums.asp.net/t/1642656.aspx/1

问题描述:在_ViewStart文件中使用ViewBag对象

解决方案:(可通过此方法根据特定参数来切换布局页实现多语言网站设计)

设置:ViewContext.Controller.ViewBag.StoreName = "My New Store";

使用:@ViewContext.Controller.ViewBag.StoreName

Generally, ViewData["StoreName"] is same as ViewBag.StoreName

Also, Controller.ViewData["StoreName"] = Controller.StoreName = ViewContext.Controller.ViewBag.StoreName =ViewContext.Controller.ViewData["StoreName"]

But every view and partial view gets its own instance of viewdata.

 

 

转载于:https://www.cnblogs.com/lushuicongsheng/archive/2012/08/16/2642276.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值