5

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.

Michael Paulukonis
  • 9,020
  • 5
  • 48
  • 68
  • 1
    If your web app is not the root of your site it matters. – Mike Cheel Sep 02 '14 at 13:57
  • @MikeCheel - that's what ~/ is for, isn't it? Is tilde-by-itself the same thing as tilde-slash? – Michael Paulukonis Sep 03 '14 at 17:26
  • The tilde means root of your application not root of the website so in many cases when they are the same it won't make a difference if you use Url.Content. – Mike Cheel Sep 03 '14 at 17:27
  • Okay, I do (now) get that application-root can be different from website root [its not in my particular case NOW, but that's what for Url.Content is for, isn't it], but I thought that tilde-slash was application root. Is tilde also application-root? Is this not the case? Is there a difference between tilde-slash and tilde-by-itself. This is what I can't find documented. – Michael Paulukonis Sep 03 '14 at 17:30
  • just tilde. I did come across a post however that says that UrlContent is no longer needed in MVC 4 when using Razor and if Razor sees the ~ it will adjust the url accordingly. http://beletsky.net/2012/04/new-in-aspnet-mvc4-razor-changes.html – Mike Cheel Sep 03 '14 at 17:32
  • Also, as far as documentation on the tilde, see this: http://msdn.microsoft.com/en-us/library/system.web.virtualpathutility(v=vs.110).aspx – Mike Cheel Sep 03 '14 at 17:33

2 Answers2

11

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

Mike Cheel
  • 12,626
  • 10
  • 72
  • 101
  • 1
    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 http://msdn.microsoft.com/en-us/library/system.web.mvc.urlhelper.content%28v=vs.118%29.aspx – Michael Paulukonis Sep 03 '14 at 17:44
  • 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. – Mike Cheel Sep 03 '14 at 17:45
  • 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("~")', – Michael Paulukonis Sep 03 '14 at 17:48
  • 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. – Mike Cheel Sep 03 '14 at 17:51
  • 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= – Michael Paulukonis Sep 03 '14 at 17:54
  • 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? – Mike Cheel Sep 03 '14 at 17:56
  • 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 Sep 03 '14 at 18:00
  • Not that I know of. I found this though but don;t know how well it works: http://john.katsiotis.com/blog/razorjs---write-razor-inside-your-javascript-files – Mike Cheel Sep 03 '14 at 18:10
3

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

Community
  • 1
  • 1
Erik Philips
  • 53,428
  • 11
  • 128
  • 150
  • 2
    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). – Michael Paulukonis Sep 03 '14 at 18:01