ItsJason
ItsJason

Reputation: 747

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 by 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"

Upvotes: 2

Views: 421

Answers (1)

DanielB
DanielB

Reputation: 20240

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.

Upvotes: 4

Related Questions