开发者

ASP.NET MVC - What is a use case of Url.Content()?

I cannot, for the life of me, think of any need for this function. MSDN says that it "Converts a virtual (relative) path to an application absolute path.". My question is, why are you using a relative path in the first place?

To illustrate, say you have a file at Scripts/awesome.js. You could get to this b开发者_运维知识库y doing

<script src="@Url.Content("~/Scripts/awesome.js")"></script>

But you could just as easily get to it using the path directly:

<script src="/Scripts/awesome.js"></script>

The same holds true for any images, CSS or any other static file you might need to reference. Can anyone show me a case where this function is needed?

EDIT:

Thanks for the answer. The reason I was looking at this is that when I try to do a rewrite to a path at a different depth, this is what breaks the most. See ASP.NET MVC UrlHelper.GenerateUrl exception: "Cannot use a leading .. to exit above the top directory"


If your application is running as a sub app (running the site within a folder), you may have a path like http://server.com/app/... In this case an absolute path to /Scripts/... will fail.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜