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

2

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.

Chevalier answered 2/9, 2014 at 13:54 Comment(6)
If your web app is not the root of your site it matters.Penetralia
@MikeCheel - that's what ~/ is for, isn't it? Is tilde-by-itself the same thing as tilde-slash?Chevalier
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.Penetralia
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.Chevalier
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. beletsky.net/2012/04/new-in-aspnet-mvc4-razor-changes.htmlPenetralia
Also, as far as documentation on the tilde, see this: msdn.microsoft.com/en-us/library/…Penetralia
P
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

Penetralia answered 3/9, 2014 at 17:35 Comment(8)
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/…Chevalier
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.Penetralia
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("~")',Chevalier
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.Penetralia
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=Chevalier
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?Penetralia
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.Chevalier
Not that I know of. I found this though but don;t know how well it works: john.katsiotis.com/blog/…Penetralia
B
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

Bath answered 3/9, 2014 at 17:47 Comment(1)
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).Chevalier

© 2022 - 2024 — McMap. All rights reserved.