本文介绍了使用@ 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 :

从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(“〜")有好处吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 20:25