剃须刀替换的优点(@href)

发布于 2025-01-08 11:27:29 字数 382 浏览 2 评论 0原文

在 ASP.NET MVC 的文档中说,当视图中有链接时,您应该执行类似的操作

<a href="@href("~/SubPage")">Subpage</a>.

razor 引擎将 @href("~/SubPage") 替换为 /Subpage< /代码>。

这样做的好处是什么?

<a href="/SubPage">Subpage</a>.

在这种情况和其他情况下(例如创建表单)为什么要使用剃刀引擎而不是直接编写您想要的内容。我认为在服务器端直接打印一些东西让引擎生成它会更快。

In the documentation of ASP.NET MVC says that you should do something like this when have a link in a view

<a href="@href("~/SubPage")">Subpage</a>.

The razor engine replaces @href("~/SubPage")to /Subpage.

What is the advantage of do it this way instead

<a href="/SubPage">Subpage</a>.

In cases like this and in others (like creating a form) why use the razor engine instead of write directly what you want. I think is faster on the server side to print something directly that let the engine to generate it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

娜些时光,永不杰束 2025-01-15 11:27:29

如果您的应用程序在子文件夹中运行,Razor @href 将创建正确的链接,如下所示:

www.myapp.com/subfolder/SubPage

如果您自己编写,您的链接将如下所示并且不起作用:

www.myapp.com/SubPage

那是因为 ~ 将被替换通过 Razor 获取您的应用程序根目录。

If your application runs in a subfolder, the Razor @href will create the correct link like this:

www.myapp.com/subfolder/SubPage

If you write it by yourself your link will be like this and will not work:

www.myapp.com/SubPage

Thats because ~ will be replaced with your application root by Razor.

决绝 2025-01-15 11:27:29

ActionLink 方法不能采用 HTML

您需要制作一个普通的 标记,并使用 @Url.Action(...) 作为 href

<a href="@Url.Action("ActionName", "ControllerName")">Link Text</a>.

The ActionLink method cannot take HTML.

You need to make a normal <a> tag, and use @Url.Action(...) for the href.

<a href="@Url.Action("ActionName", "ControllerName")">Link Text</a>.
冷情妓 2025-01-15 11:27:29

我只是写

Subpage

我无法意识到任何负面后备

I simply write

<a href="SubPage">Subpage</a>

I can't realize any negative fallback

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文