Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a difference between Url.Content("~/...") and "~/" for urls in ASP.NET MVC?

I'm dealing with a web application that resides within a subdirectory on a domain, and I'm attempting to discern the most idiomatic way of inserting a proper URL into an img tag. While the following both produce the same HTML on the client machine, I'm not sure which is more "correct"

  • <img src="~/Content/images/blah.png" />
  • <img src="@Url.Content("~/Content/images/blah.png")

Both of these produce an absolute path of /subfolder/Content/images/blah.png, so both work, but I'm curious which one is the right way of doing it.

Is there any difference between these two approaches (for example, is one being resolved by a different mechanism than the other?), or is the former just syntactic sugar for the latter?

like image 815
Adam Robinson Avatar asked Nov 07 '12 13:11

Adam Robinson


People also ask

How Routing works in MVC?

In MVC, routing is a process of mapping the browser request to the controller action and return response back. Each MVC application has default routing for the default HomeController. We can set custom routing for newly created controller.

Which is the default route pattern in MVC?

The default route table contains a single route (named Default). The Default route maps the first segment of a URL to a controller name, the second segment of a URL to a controller action, and the third segment to a parameter named id. The Default route maps this URL to the following parameters: controller = Home.

What is URL content?

A URL (Uniform Resource Locator) or webpage address, is a reference to a web resource that specifies its exact location online (within a computer network). A content URL refers to a webpage address that specifically relates to certain content on the internet, such as an image or a video file.

What is URL in ASP.NET MVC?

In the ASP.NET Web Forms application, every URL must match with a specific . aspx file. For example, a URL http://domain/studentsinfo.aspx must match with the file studentsinfo. aspx that contains code and markup for rendering a response to the browser.


1 Answers

With MVC4 you no longer need @Url.Content

If Razor detects ~/ it would created an output identical to @Url.Content.

http://www.beletsky.net/2012/04/new-in-aspnet-mvc4-razor-changes.html

like image 200
Curtis Avatar answered Sep 20 '22 09:09

Curtis