简体   繁体   English

如何处理从开发到生产环境的Ajax URL更改?

[英]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.

相关问题 如何编辑开发和生产的环境变量? - How to edit environment variable for development and production? 如何有条件地使用开发或生产环境变量 - How to conditionally use a development or production environment variable webpack-如何在生产和开发环境中使用不同的变量 - webpack - how to have different variable for production and development environment 我怎么知道Vue.js环境是开发环境还是生产环境? - How can I know the Vue.js environment is development or production? 这是如何基于从URL获得值来处理两个Ajax请求? - Is this how to handle two ajax request based on get value from url? 在 Nuxt 中设置开发或生产 api URL from.env - Set development or production api URL from .env in Nuxt 在生产与开发环境中处理javascript聚合 - Handling javascript aggregation in production vs development environment 从开发环境切换到生产环境时,使用grunt切换HTML元素 - Use grunt to switch HTML elements when switching from development to production environment 如何在 SAP AJAX Webapp 中处理带参数的 URL? - How to handle URL with parameters in a SAP AJAX Webapp? 如何在app.js中获取主机以用于确定环境(生产/开发) - How to get host in app.js for use in determining environment (production/development)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM