英文:
What does @(Page.NavId ..) Do?
问题
我继承了一个我正在用C#/Razor开发的网站的代码,不太明白在_Layout.cshtml页面的以下行中,以下代码@(Page.NavID...)是做什么的:
<ul class="nav navbar-nav">
<li @(Page.NavId == "Page1" ? @Html.Raw("class='active'") : @Html.Raw(""))>
<a href="~/Page1.cshtml">
<span class="glyphicon glyphicon-folder-open"</span> Page 1内容
</a>
</li>
我理解Lambda表达式,但我在哪里找到NavId的定义?这是Page对象的一个方法吗?任何帮助都将不胜感激。
英文:
I have inherited code for a website I am developing in C#/Razor, and don't exactly understand what the following code @(Page.NavID...) does in the following line in the _Layout.cshtml page:
<ul class="nav navbar-nav">
<li @(Page.NavId == "Page1" ? @Html.Raw("class='active'") : @Html.Raw(""))>
<a href="~/Page1.cshtml">
<span class="glyphicon glyphicon-folder-open"</span> &nbsp;&nbsp; Page 1 content
</a>
</li>
I understand the Lambda expression, but where do I find the definition for NavId? Is this a method off of the Page object??? Any help appreciated.
答案1
得分: 1
这看起来像一个Web Pages应用程序,而不是一个Razor Pages应用程序。 NavId
不是该框架的API的一部分,所以它可能是由原始开发人员实现的一个基类的属性,该基类本身从WebPage
派生。
如果F12无法帮助您找到定义,您可以尝试搜索应用程序文件的内容。
根据您发布的代码,我建议NavId
代表当前页面的名称(可能是从数据库获取的)。 这个三元表达式(它不是一个lambda)会在当前页面为“Page1”时将列表项的CSS类切换为“active”。
在该代码中,没有必要使用Html.Raw
方法调用。以下是更简洁的写法:
<li class="@(Page.NavId == "Page1" ? "active" : null)">
英文:
That looks like a Web Pages app, not a Razor Pages app. NavId
was not part of the API for that framework, so it is possibly a property of a base class implemented by the original developers that itself derives from WebPage
.
If F12 doesn't help you locate the definition, you could try searching the contents of the application files.
From the code you posted, I suggest that NavId
represents the name of the current page (possibly obtained from a database). The ternary expression (it's not a lambda) switches the CSS class of the list item to "active" if the current page is "Page1".
There is no need for the Html.Raw
methods calls in that code. It would have been better (more concise) as follows:
<li class="@(Page.NavId == "Page1" ? "active" : null)">
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论