开发者

Best practice on where to include javascript runtime dependent code in ASP.NET MVC apps

I have some javascript code that has in it localized messages tha开发者_StackOverflow社区t i pull from my resource files. My problem is that if i include my javascript files like...

<script scr="..." type="text/javascript"/>

in my masterpage for example, then the <%= Resources... %> code is not running on view rendering. My current way of resolving this problem is by including the full code inside script tags in my masterpage, but i don't like it.

Any better suggestions?


You need to close out all script tags as such for IE to accept the JavaScript:

<script>....</script>

Not

<script .... />


Understanding the problem

Inline asp.net tags (e.g. '<%= %>' are parsed at runtime when the server renders your page. In order for ASP.Net to be able to parse documents it needs to be specified by the webserver that the extension is handled by the aspnet_isapi ISAPI extension.

The regular JavaScript files, to which you are adding asp.net code, will never run through this extension and thus will never be 'compiled' properly.

Now is there a solution?

Mads Kristensen has written a blogpost about this problem in 2008. It is still valid afaik and should do exactly what you need. See http://madskristensen.net/post/Localize-text-in-JavaScript-files-in-ASPNET.aspx

He uses the .axd extension which is also handled by the aspnet_isapi ISAPI extension.

Another very good post which addresses the problem more specifically for asp.net mvc can be found here: http://afana.me/post/aspnet-mvc-internationalization.aspx

Hope this helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜