[英]How to handle Ajax URL changes from a development to production environment?
The AJAX URLs used in my project change in different environments ie dev/test and production as shown below: 我的项目中使用的AJAX URL在不同的环境中发生变化,即开发/测试和生产,如下所示:
dev/test: 开发/测试:
http://localhost:60000/Users/GetAll
HTTP://本地主机:60000 /用户/ GETALL
production: 生产:
http://production.com/myProject/Users/GetAll
http://production.com/myProject/Users/GetAll
What would be the best way of tackling this? 解决这个问题的最佳方法是什么?
My project is an MVC project but I will not use HTML Helpers to generate a URL as I don't want to mix C# with my JS script 我的项目是MVC项目,但是我不想使用HTML Helpers来生成URL,因为我不想将C#与我的JS脚本混合使用
You can use this Visual studio extension Slow Cheetah https://github.com/Microsoft/slow-cheetah 您可以使用此Visual Studio扩展Slow Cheetah https://github.com/Microsoft/slow-cheetah
Actually this plugin changes the web.config file upon application publish. 实际上,此插件会在应用程序发布时更改web.config文件。 In your web.config you can store the 2 different API endpoints.
在您的web.config中,您可以存储2个不同的API端点。
On your Dev Web.config 在您的Dev Web.config上
<appSettings>
<add key="Endpoint" value="http://localhost:60000" />
</appSettings>
On Production 生产中
<appSettings>
<add key="Endpoint" value="http://production.com/myProject/" />
</appSettings>
Then you can get you endpoint like this 然后您可以像这样获得端点
System.Configuration.ConfigurationManager.AppSettings.Get("Endpoint").ToString();
So this value should come from server side, You define it in configs somewhere on server and based on which env is fired up ( Read conditional check based on env thread being executed) , you send that value/variable somehow down to the client app which can easily parse that information in app.js/whatever.js... ( start file of project) and make that a global variable for app from there on to use in your API calls. 因此,该值应该来自服务器端,您可以在服务器上某个位置的配置中定义它,并根据它触发哪个env(基于正在执行的env线程读取条件检查),然后将该值/变量以某种方式发送给客户端应用,可以轻松解析app.js / whatever.js ...(项目的开始文件)中的信息,并从那里将其设为应用程序的全局变量,以供您在API调用中使用。 I haven't worked on C# but the approach has to be same.
我没有从事C#工作,但方法必须相同。
Node.js gives this value in it's process as process.env. Node.js在它的进程中将此值称为process.env。 That simple!
这么简单!
You should really not hardcode this in client anywhere. 您真的不应该在任何地方的客户端中对此进行硬编码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.