5

I have a JavaScript function like this in my ASP.NET MVC 3 application.

function Foo()
{
   var url = "@Url.Action("MyAction", "MyController")";
   alert(url);
}

I'm confused as to why the above code works. I was expecting to have to escape @Url.Action(...) inside <%= tags?

3 Answers 3

4

This is a feature of the Razor view engine - it handles the switch between HTML/Javascript and your server-side view code so you don't need to escape it in most cases.

See Scott Guthrie's blog post on it: http://weblogs.asp.net/scottgu/archive/2010/07/02/introducing-razor.aspx

Sign up to request clarification or add additional context in comments.

1 Comment

Ah, that's the link I was looking for. Google fail
2

Looks like you're using the new Razor view engine.

Edit: I'm assuming the JavaScript function is written inside a view file (cshtml)

4 Comments

Indeed I am, is this a feature?
Wow, what a pleasant surprise then :-) Stumbled on it purely by accident via a typo! Thanks.
Phil, yes...the javascript is inside one of my views.
0

Yes ,It works in razor view pages,but the intellisense don't work inside the "" or '' as webform view engine.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.