asp.net-mvc – Razor _Layout.cshtml中的嵌入代码
发布时间:2020-12-16 06:30:54 所属栏目:asp.Net 来源:网络整理
导读:我正在研究一个MVC3 Razor Web应用程序,它从 java内容管理系统获取它的页面装饰.由于每个页面都共享这个装饰,我将CMS内容的检索放在_Layout.cshtml文件中,但我对我实现的代码并不完全满意…… !DOCTYPE htmlhtmlhead meta charset="utf-8" / @{ -- The first
我正在研究一个MVC3 Razor Web应用程序,它从
java内容管理系统获取它的页面装饰.由于每个页面都共享这个装饰,我将CMS内容的检索放在_Layout.cshtml文件中,但我对我实现的代码并不完全满意……
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> @{ -- The first two lines are temporary and will be removed soon. var identity = new GenericIdentity("","",true); var principal = new GenericPrincipal(identity,new string[] { }); var cmsInterface = MvcApplication.WindsorContainer.Resolve<ICMSInterface>(); cmsInterface.LoadContent(principal,2); } @Html.Raw(cmsInterface.GetHeadSection()) </head> <body> @Html.Raw(cmsInterface.GetBodySection(0)) @RenderBody() @Html.Raw(cmsInterface.GetBodySection(1)) </body> </html> 由于_layout文件没有控制器,我无法看到我可以将代码放在哪里进行检索.以下是我考虑过的一些事情: >以单独的部分检索CMS内容,因此我不需要LoadContent调用.不幸的是,由于我必须用来检索CMS内容的组件,这是不可能的,它是全有或全无. 有没有人有任何其他建议/意见? 解决方法
您可以使用全局操作筛选器将所需数据添加到所有控制器中的ViewBag:
public class LoadCmsAttribute : ActionFilterAttribute { public override void OnResultExecuting(ResultExecutingContext filterContext) { if (!filterContext.IsChildAction && !filterContext.HttpContext.Request.IsAjaxRequest() && filterContext.Result is ViewResult) { var identity = new GenericIdentity("",true); var principal = new GenericPrincipal(identity,new string[] { }); var cmsInterface = MvcApp.WindsorContainer.Resolve<ICMSInterface>(); cmsInterface.LoadContent(principal,2); var viewBag = filterContext.Controller.ViewBag; viewBag.HeadSection = cmsInterface.GetHeadSection(); viewBag.FirstBodySection = cmsInterface.BodySection(0); viewBag.SecondBodySection = cmsInterface.BodySection(1); } } } Global.asax中: public static void RegisterGlobalFilters(GlobalFilterCollection filters) { ... filters.Add(new LoadCmsAttribute()); } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-web-api – 使用swashbuckle api文档的http basic
- asp.net-mvc – 如何根据枚举值检查单选按钮
- asp.net-mvc – 在自定义Html帮助器中访问模型对象
- mvc项目架构分享系列之架构搭建之Repository和Service
- asp.net-mvc-4 – 基于角色的导航
- asp.net-mvc – Ninject绑定可以基于URL /路由值吗?
- asp.net-mvc – MVC的HTTP缓存 – Last-Modified响应头始终
- asp.net-mvc – 如何使用AngularJs显示MVC登录的用户名
- 深度探秘.NET 5
- asp-classic – 使用ADO Stream逐行读取大文件?