最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

asp.net mvc - @ symbol in JavaScript block - Stack Overflow

programmeradmin2浏览0评论

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?

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?

Share Improve this question edited Nov 11, 2010 at 0:35 p.campbell 101k70 gold badges262 silver badges326 bronze badges asked Nov 11, 2010 at 0:06 SenkweSenkwe 2,2563 gold badges24 silver badges34 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 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/scottgu/archive/2010/07/02/introducing-razor.aspx

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

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

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

发布评论

评论列表(0)

  1. 暂无评论