简体   繁体   English

使用Asp.Net MVC在javascript中删除硬编码字符串的最佳方法

[英]Best way to remove hardcoded string in javascript with Asp.Net MVC

In my javascript files, I have too much hardcorded url that references controllers actions. 在我的javascript文件中,我有太多引用控制器操作的hardcorded url。 Sometime, I also have messages displayed directly from my javascript. 有时,我也会直接从我的javascript中显示消息。

What is the best way to remove all these hardcoded strings from javascript files? 从javascript文件中删除所有这些硬编码字符串的最佳方法是什么?

  1. Step one , use T4MVC to automatically generate a structured set of .NET classes that describe your ASP.NET MVC's application structure 第一步 ,使用T4MVC自动生成一组结构化的.NET类,用于描述ASP.NET MVC的应用程序结构

  2. Step two , create a new partial view that defines server-side information described by T4MVC as a set of Javascript constants. 第二步 ,创建一个新的局部视图,将T4MVC描述的服务器端信息定义为一组Javascript常量。

     <script type="text/javascript"> var SHOPPING_CART_DETAIL_URL = '@Url.Action(MVC.ShoppingCart.Detail(Model.ShoppingCartId))'; var CLIENT_DETAIL_URL = '@Url.Action(MVC.Client.Detail(Model.ClientId))'; var USER_IS_ADMIN = @(User.IsInRole(Roles.Admin) ? "true" : "false"); </script> 
  3. Step three , include this partial view in the head of whatever page you need it. 第三步 ,将这个局部视图包含在您需要的任何页面的头部。 You could also include it in the head of your general page layout. 您还可以将其包含在常规页面布局的头部。 Make sure this loads before the rest of your JavaScript files. 确保在其余JavaScript文件之前加载。

  4. Step four , use your newly defined JavaScript constants throughout your JavaScript files. 第四步 ,在整个JavaScript文件中使用新定义的JavaScript常量。

For that purpose I use T4MVC . 为此,我使用T4MVC It will allow you to use strongly typed objects in place of literal strings. 它允许您使用强类型对象代替文字字符串。 You will need to initialize your javascript in your views, but other than that it works great. 您需要在视图中初始化您的javascript,但除此之外它的效果很好。

For messages etc. here are some ideas: what are the different approaches to multilingual javascript applications 对于消息等,这里有一些想法: 多语言javascript应用程序的不同方法是什么

If you want to avoid using T4MVC, you could make a Controller that parses JS files. 如果您想避免使用T4MVC,您可以创建一个解析JS文件的Controller。 Just configure a route in Global.asax that catches all the javascript urls, and that action will ready the JS file, parse it, return a result with the URLs. 只需在Global.asax中配置一个捕获所有javascript url的路由,该操作将准备好JS文件,解析它,返回带有URL的结果。

        routes.MapRoute(
            "Javascript",
            "{url}.js",
            new { controller = "Javascript", action = "Parse" }
        );

Then write an action that reads the requested URL, finds the JS file, replaces values based on a Key/Value dictionnary. 然后编写一个读取请求的URL的动作,找到JS文件,根据键/值字典替换值。 Perhaps even externalize that Key/Value dictionnary if could need that. 如果需要,可能甚至将Key / Value词典外化。 The rest is up to you. 剩下的由你决定。

Another option would be to use a controller action that returns Javascript code. 另一种选择是使用返回Javascript代码的控制器操作。 That javascript code would be a list of variables filled with URLs that are MVC generated. 该javascript代码将是填充了MVC生成的URL的变量列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM