Both Href
and Url.Content
can be used to resolve a virtual path, which is relative to the virtual root path ~, of files or folders.
What is the difference between Href and Url.Content ?
Both the link text and the URL are defined within an HTML anchor element <a> . The HTML anchor element contains a href attribute with a value as its source — and the value is the URL that takes the person clicking on it to the destination you have defined.
Url.Content is used when you wish to resolve a URL for any file or resource on your site and you would pass it the relative path: @Url.Content("~/path/file.htm") Url.Action is used to resolve an action from a controller such as: @Url.Action("ActionName", "ControllerName", new { variable = value })
HREF - Hyperlink Reference | Often used in tags to reference another page or web link. SRC - Source | Often used in tags to give the web page the source of an image, video or file.
The href attribute specifies the location (URL) of a resource, that an anchor element points to, or the location of a linked resource, like a stylesheet. While the src attribute is used to embed a resource - usually URLs - into a document, think embedded images ( <img> ) and <script> tags.
url.content does alot more than just creating an normal href, it takes into context the application's sub/virtual directory, whereas href it's a straight pointer.
for best practice use url.content, this way you know that if your site moves into a sub/virtual directory, your link will still work.
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