Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@ symbol in JavaScript block

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?

like image 907
Senkwe Avatar asked Nov 11 '10 00:11

Senkwe


2 Answers

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

like image 167
Jon Galloway Avatar answered Sep 28 '22 05:09

Jon Galloway


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

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

like image 31
Phil Avatar answered Sep 28 '22 05:09

Phil