Asp.net core 根据Razor页面中的路由有条件地呈现html

Asp.net core 根据Razor页面中的路由有条件地呈现html,asp.net-core,razor,razor-pages,Asp.net Core,Razor,Razor Pages,假设我有以下Razor文件\u Layout.cshtml。使用ASP.NET Core 3+和Razor页面时,如何编写注释条件 <ul class="navbar-nav"> <li class="nav-item"> <a class="nav-link" asp-page="/Index">Index</a> </li> @* If route starts with /a/, for

假设我有以下Razor文件
\u Layout.cshtml
。使用ASP.NET Core 3+和Razor页面时,如何编写注释条件

<ul class="navbar-nav">
    <li class="nav-item">
        <a class="nav-link" asp-page="/Index">Index</a>
    </li>

    @* If route starts with /a/, for example http://localhost/a/1.html and http://localhost/a/2/1.html *@
    <li class="nav-item">
        <a class="nav-link" asp-page="/a/Index">A</a>
    </li>

    @* If route starts with /b/, for example http://localhost/b/1.html and http://localhost/b/2/1.html *@
    <li class="nav-item">
        <a class="nav-link" asp-page="/b/Index">B</a>
    </li>
    <li class="nav-item">
        <a class="nav-link" asp-page="/b/1/Index">B other</a>
    </li>
</ul>
  • 指数
  • @*例如,如果路由以/a/开头http://localhost/a/1.html 及http://localhost/a/2/1.html *@
  • A.
  • @*例如,如果路由以/b/开头http://localhost/b/1.html 及http://localhost/b/2/1.html *@
  • B
  • B其他

在Razor页面中,也可以在_布局文件中使用
ViewContext.RoutedData.Values[“page”]
。如果URL是例如
https://localhost:44359/Help/About
然后它将生成
/Help/About

使用您的代码:

@{
    string page = ViewContext.RouteData.Values["page"] as string; // Values[] produces objects, so cast is needed
}

<ul class="navbar-nav">
    <li class="nav-item">
        <a class="nav-link" asp-page="/Index">Index</a>
    </li>

    @if (page.StartsWith("/a/"))
    {
        <li class="nav-item">
            <a class="nav-link" asp-page="/a/Index">A</a>
        </li>
    }

    @if (page.StartsWith("/b/"))
    {
        <li class="nav-item">
            <a class="nav-link" asp-page="/b/Index">B</a>
        </li>
        <li class="nav-item">
            <a class="nav-link" asp-page="/b/1/Index">B other</a>
        </li>
    }
</ul>
@{
string page=ViewContext.RouteData.Values[“page”]作为字符串;//Values[]生成对象,因此需要强制转换
}
  • 指数
  • @如果(第页开始,以(“/a/”)开头) {
  • A.
  • } @如果(第页开始,带(“/b/”) {
  • B
  • B其他
  • }

您可以为特定文件夹中的所有Razor页面执行Razor页面约定。使用此约定,可以设置操作筛选器(结果筛选器)以向ViewData添加标识符

过滤器:

class AddRouteIdentifierFilter:ResultFilterAttribute
{
私有只读字符串标识符;
公共测试筛选器(字符串标识符)
{
this.identifier=标识符;
}
public override void OnResultExecuting(ResultExecutingContext)
{
base.OnResultExecuting(上下文);
((PageResult)context.Result).ViewData[“路由标识符”]=标识符;
}
}
制定公约:

services.AddMvc().AddRazorPagesOptions(opt=>
opt.Conventions.AddFolderApplicationModelConvention(“/A”,model=>
model.Filters.Add(新的AddRouteIdentifierFilter(“a”);
);
opt.Conventions.AddFolderApplicationModelConvention(“/B”,model=>
model.Filters.Add(新的AddRouteIdentifierFilter(“b”);
);
);
访问Razor页面视图中的标识符:

<h1>Identifier: @ViewData["route-identifier"]</h1>
标识符:@ViewData[“路由标识符”]

不确定这是否能解决您的问题,但看起来您可以向
视图状态添加自定义值,并基于此显示/隐藏元素。请参阅(此处他们更改了整个布局文件,但它也适用于单个元素)。@rm代码可以工作,但我认为有一种简单的方法可以访问Razor文件中的当前路由。假设没有,安全吗?