使用@ Url.Content(“〜")有好处吗? [英] Is there a benefit to using @Url.Content("~")

查看:366
本文介绍了使用@ Url.Content(“〜")有好处吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是MVC4/razor2的新手,我想了解使用@Url.Content@Url.Action的一般好处-如果更改路由或虚拟目录,则可以正确呈现magic-url-strings.

I'm new to MVC4/razor2, and I think understand the general benefit of using @Url.Content and @Url.Action- if my routing or virtual directory changes, magic-url-strings are correctly rendered.

我正在用'@Url.Content("~")'标记的视图中查看一些带有剃刀的旧版JavaScript代码.呈现为'/'-或网站根目录.哪个……总是这样,不是吗?

I'm looking at some legacy Javascript-with-razor code in a view that is peppered with '@Url.Content("~")'. This renders out as '/' - or, website root. Which.... would always be the case, no?

还是在某些情况下可以用不同的方式呈现?

Or is there some situation in which this could be rendered differently?

注意:它不是不是 ~/-只是普通的波浪号.

Note: it is not ~/ - just plain ol' tilde.

我正计划提取对辅助函数的razor调用,并将JavaScript的主要块移到外部文件中(以减少毛刺和一般的清洁度").我不需要修复"当前正在发生的任何事情,但是我想更好地理解它.

I'm planning on extracting the razor calls to helper-functions, and moving main block of JavaScript into an external file (for linting and general "cleanliness"). I don't need to "fix" anything that currently happening, but I would like to understand it better.

推荐答案

Url.Content将代字号映射到应用程序根目录.应用程序根目录与网站根目录不同.

Url.Content maps the tilde to the application root. The application root is not the same thing as the website root.

从本文 http://msdn.microsoft .com/en-us/library/system.web.virtualpathutility(v = vs.110).aspx :

绝对虚拟路径以文字斜杠(/)开头.一种 相对虚拟路径是相对于应用程序根目录的, 如果它只是一个波浪号(〜)或以波浪号和一个双精度数开头 反斜杠(〜\)或代字号和斜杠标记(〜/).制作虚拟 相对路径使路径独立于应用程序.

An absolute virtual path starts with the literal slash mark (/). A relative virtual path is relative to the application root directory, if it is just a tilde (~) or starts with the tilde and a double backslash (~\) or the tilde and a slash mark (~/). Making a virtual path relative makes the path independent of the application.

从MVC4 Url开始,不需要将波浪号转换为应用程序根目录的内容:

As of MVC4 Url.Content is not needed to convert the tilde to the applicaiton root: http://beletsky.net/2012/04/new-in-aspnet-mvc4-razor-changes.html

这篇关于使用@ Url.Content(“〜")有好处吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆