@(Page.NavId ..) 是什么作用?

huangapple go评论69阅读模式
英文:

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> &nbsp;&nbsp; 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:

&lt;ul class=&quot;nav navbar-nav&quot;&gt;
  &lt;li @(Page.NavId == &quot;Page1&quot; ? @Html.Raw(&quot;class=&#39;active&#39;&quot;) : @Html.Raw(&quot;&quot;))&gt;
    &lt;a href=&quot;~/Page1.cshtml&quot;&gt;
      &lt;span class=&quot;glyphicon glyphicon-folder-open&quot;&lt;/span&gt; &amp;nbsp;&amp;nbsp; Page 1 content
    &lt;/a&gt;
  &lt;/li&gt;

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:

&lt;li class=&quot;@(Page.NavId == &quot;Page1&quot; ? &quot;active&quot; : null)&quot;&gt;

huangapple
  • 本文由 发表于 2023年7月17日 21:56:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76705173.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定