c# - 如何在 Asp.NET Core 中呈现不同的布局
问题描述
我想在我的 asp.net 核心应用程序的某些页面上呈现空布局。为此,我在_ViewStart.cshtml
.
@{
var controller = HttpContext.Current.Request.RequestContext.RouteData.Values["Controller"].ToString();
string cLayout = "";
if (controller == "Empty")
{
cLayout = "~/Views/Shared/_Empty_Layout.cshtml";
}
else
{
cLayout = "~/Views/Shared/_Layout.cshtml";
}
Layout = cLayout;
}
此代码工作正常,Asp.NET MVC App
但它在.NET Core App
. 错误是The name 'HttpContext' does not exist in the current context
解决方案
HttpContext.Current
这是微软的一个非常糟糕的想法,幸运的是,它没有迁移到 ASP.NET Core。
您可以RouteData
像这样访问:
@Url.ActionContext.RouteData.Values["Controller"]
// or
@ViewContext.RouteData.Values["Controller"]
也就是说,“空布局”听起来你根本不想要布局。如果是这种情况,请使用:
@{
var controller = ViewContext.RouteData.Values["Controller"].ToString();
string layout = null;
if (controller != "Empty")
{
layout = "~/Views/Shared/_Layout.cshtml";
}
Layout = layout;
}
null
这里的意思是“不要使用布局”。
推荐阅读
- reactjs - React Js + bootstrap 双语应用 rtl 问题
- java - 未能在项目 hapi-fhir-jpaserver-starter 上执行目标 org.apache.maven.plugins:maven-war-plugin:3.2.3:war (default-war)
- javascript - 浏览器上的 S3 多部分下载
- python - tkinter 中是否有一些用于返回键的新关键字?
- javascript - 动态添加网格会导致控件滞后
- image - BIRT 报表设计器 - 更改缺失动态图像的文本
- linux - 如何在 stunnel 上挂载共享文件夹 cifs?(安全 samba)
- android - Outlook on Android (11) 图像大小爆炸式增长
- postgresql - 验证 postgres 表中的数据
- python - 是否有计算数据框python中每个月的百分比变化的功能