Pascal
Pascal

Reputation: 12885

Is there a difference in these javascript include definitions

Both include the same files. Both works but is there any advantage favoring the one over the other?

<script type="text/javascript" src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")"></script> 

<script type="text/javascript" src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>

Upvotes: 2

Views: 207

Answers (2)

Shawn C.
Shawn C.

Reputation: 6841

In the first case the Url.Content converts a relative path to an application absolute path

In the second case the string is considered a literal and outputted link will contain the ~/. Which in most cases would not work because it will look for a folder named ~ under the current directory.

But in asp.net mvc 4 the razor viewengine will see the ~/ and do the Url.Content for you automatically.

http://www.davidhayden.me/blog/asp.net-mvc-4-the-new-tilde-slash-feature-in-razor-2

Upvotes: 3

vol7ron
vol7ron

Reputation: 42149

I believe there is a minimal amount of more work in the first, because the compiler finds @Url.Content and calls that function.

The end result is the same, but I think there are 2-3 more steps more in the first. All of that will happen in [milli-nano]seconds, though.

Upvotes: 0

Related Questions