考虑在 Visual Studio 开发环境与部署的 IIS 生产或测试环境中需要将 $.post()
转换为略有不同的 URL 结构。
部署到测试服务器时,应用程序在 IIS 中的虚拟目录下运行。该 URL 将类似于:
部署的
网址:http://myServer/myApplication/Area/Controller/Action
使用 jQuery 的 .post()
,我需要提供字符串:
$.post( "/myApplication/myArea/myController/myMethod"
开发
在 Visual Studio 环境中时
卡西尼号网址是:
http://localhost:123/Area/Controller/Action
使用 jQuery 的
.post()
,我需要提供字符串: $.post( "/myArea/myController/myMethod"
问题:
无论部署的环境如何,如何使它们都使用相同的代码行?
最佳答案
我这样做的方法是从 RouteUrl
方法生成 url,如下所示:
var url = "<%= Url.RouteUrl(new { area="myArea", controller = "controller", action = "actionmethod" }) %>";
$.post(url ...
只要您的路由设置正确,就会生成适当的 Url。
编辑: 现在无需修改即可处理区域。
关于jQuery ASP.NET MVC - $.post() 到不同环境中的不同 URL 路径,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/1527129/