Is there a benefit to using @Url.Content("~")

10,580

Solution 1

Url.Content maps the tilde to the application root. The application root is not the same thing as the website root.

From this article http://msdn.microsoft.com/en-us/library/system.web.virtualpathutility(v=vs.110).aspx:

An absolute virtual path starts with the literal slash mark (/). A relative virtual path is relative to the application root directory, if it is just a tilde (~) or starts with the tilde and a double backslash (~\) or the tilde and a slash mark (~/). Making a virtual path relative makes the path independent of the application.

As of MVC4 Url.Content is not needed to convert the tilde to the applicaiton root: http://beletsky.net/2012/04/new-in-aspnet-mvc4-razor-changes.html

Solution 2

There appear to be two separate questions, so I'll address them individually.

Is there a benefit to using @Url.Content()

As of Razor 2 there is almost no reason to use it.

The following are equivalent (for any application root):

<a href="@Url.Content("~")">Root</a>

and

<a href="~">Root</a>

Secondly

What is the ~ (tidle)

slash(/) vs tilde slash (~/) in style sheet path in asp.net

Share:
10,580

Related videos on Youtube

Michael Paulukonis
Author by

Michael Paulukonis

I've been programming professionally in C#, ASP.NET, Perl, JavaScript and VB6 (!!!) since 2003. Emacs and FireFox are my preferred environments. After that, I like to throw wikis at problems to make them go away. And if they don't go away, at least we've got the page revisions under version control! You know what they say about bad code? It's anything YOU wrote, or anything I wrote more than 6 weeks ago. :::sigh::: I keep trying to add days to that equation....

Updated on June 04, 2022

Comments

  • Michael Paulukonis
    Michael Paulukonis almost 2 years

    I'm new to MVC4/razor2, and I think understand the general benefit of using @Url.Content and @Url.Action- if my routing or virtual directory changes, magic-url-strings are correctly rendered.

    I'm looking at some legacy Javascript-with-razor code in a view that is peppered with '@Url.Content("~")'. This renders out as '/' - or, website root. Which.... would always be the case, no?

    Or is there some situation in which this could be rendered differently?

    Note: it is not ~/ - just plain ol' tilde.


    I'm planning on extracting the razor calls to helper-functions, and moving main block of JavaScript into an external file (for linting and general "cleanliness"). I don't need to "fix" anything that currently happening, but I would like to understand it better.

    • Mike Cheel
      Mike Cheel over 9 years
      If your web app is not the root of your site it matters.
  • Michael Paulukonis
    Michael Paulukonis over 9 years
    The link you posted, as well as others I've seen, seem to suggest that Url.Content is only not needed when "~" is prefixed by href= or src= , which is not always the case. In my particular, case, it's urls in an object, which just look like magic strings. Not sure how razor could find all of those cases. Url.Content is also not marked as deprecated at msdn.microsoft.com/en-us/library/…
  • Mike Cheel
    Mike Cheel over 9 years
    Remember which side of the fence we are talking about. The tilde is server side. The browser doesn't know about it so client side urls will need to be generated differently. This doesn't mean you cannot still use it for javascript however.
  • Michael Paulukonis
    Michael Paulukonis over 9 years
    Yes. Server-side. A whole bunch of javascript [4000 lines. yaaay] shoved into a view. I'm working on extracting the razor commands so I can put the JS into a standalone file. The code looks something like url: '@Url.Content("~")',
  • Mike Cheel
    Mike Cheel over 9 years
    What I have done is written it as say a jquery plugin (you can write your stuff in a separate file, the jquery stuff isn't required) where I only need to pass in my stuff as parameters. Then I have a partial view that I plug in the server generated stuff as parameters to the object (in this case jquery plugin) so most of my logic is in a separate js file and I just use razor to generate the parameters.
  • Michael Paulukonis
    Michael Paulukonis over 9 years
    Yes, that's why I'm working on extracting the razor commands so I can put the JS into a standalone file. But code that looks like url: '@Url.Content("~")' cannot be replaced by url: "~" - it's now a magic-string that razor ignores; Url.Content can only be dispensed with in case of relative paths for href= and src=
  • Mike Cheel
    Mike Cheel over 9 years
    I'm confused now. You cannot us ~ or Url.Content("~") in js files, only in server side files procesed by asp.net. Am I missing something?
  • Michael Paulukonis
    Michael Paulukonis over 9 years
    Is there any way to render razor code except in a view? I didn't explicitly say "view" because I thought "razor" made that defacto. I've added that clarification in the question.
  • Michael Paulukonis
    Michael Paulukonis over 9 years
    Erik, as I pointed out in other comments, Url.Content can only be dispensed with in the cases of href= and src= (and possible some others).
  • Mike Cheel
    Mike Cheel over 9 years
    Not that I know of. I found this though but don;t know how well it works: john.katsiotis.com/blog/…