剃须刀替换的优点(@href)
在 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技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您的应用程序在子文件夹中运行,Razor @href 将创建正确的链接,如下所示:
如果您自己编写,您的链接将如下所示并且不起作用:
那是因为
~
将被替换通过 Razor 获取您的应用程序根目录。If your application runs in a subfolder, the Razor @href will create the correct link like this:
If you write it by yourself your link will be like this and will not work:
Thats because
~
will be replaced with your application root by Razor.ActionLink
方法不能采用HTML
。您需要制作一个普通的
标记,并使用
@Url.Action(...)
作为href
。The
ActionLink
method cannot takeHTML
.You need to make a normal
<a>
tag, and use@Url.Action(...)
for thehref
.我只是写
Subpage
我无法意识到任何负面后备
I simply write
<a href="SubPage">Subpage</a>
I can't realize any negative fallback