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?
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With