简体   繁体   English

部署到IIS后,对控制器的API调用中断

[英]API Calls to controller is breaking after deploying to IIS

I am invoking API calls to get JSON data from server using javascript. 我正在调用API调用以使用javascript从服务器获取JSON数据。

$(function () {

    var customersTable = $('#Customers');
    var returnCustomersTable = UpdateDataTable(customersTable, "/Customers/Loaddata");
}

This works fine when I am working with Visual studio Dev environment , because my web application is on root and so all javascripts Works fine. 当我在Visual Studio Dev环境中工作时,这可以正常工作,因为我的Web应用程序位于root上,因此所有javascript都可以正常工作。

Example: 例:

My WebSite URL: http://localhost:4391 我的网站网址: http://localhost:4391
API Calls will be: http://localhost:4391/Customers/Loaddata API调用将是: http://localhost:4391/Customers/Loaddata

This works fine. 这很好。

But when I deploy application to IIS, my website URL will be, 但是,当我将应用程序部署到IIS时,我的网站网址将是

My WebSite URL: http://localhost/MyAppName 我的网站网址: http://localhost/MyAppName

But API calls would still be, 但是API调用仍然会
API Calls will be: http://localhost/Customers/Loaddata , which results in not found. API调用为: http://localhost/Customers/Loaddata ,导致找不到。

Since I am using javascript in a separate file , I wont be able to do URL.Action . 由于我在单独的文件中使用javascript,因此我将无法执行URL.Action

The other option would be to create a base URL for dev and prod and the append with each servcie call. 另一个选择是为dev和prod创建基本URL,并为每个servcie调用创建追加。

But I am thinking if there is any other way. 但是我在想是否还有其他方法。

The routing configuration in ASP.NET MVC is designed to serve as the single place in the application where all of the URLs can be maintained. ASP.NET MVC中的路由配置旨在用作应用程序中可以维护所有URL的单个位置。

It helps considerably with maintenance of the project if all URLs are generated using routing through one of the UrlHelper based methods, such as Url.Action() rather than hard coded in controllers and views. 如果所有URL都是通过基于UrlHelper的方法之一(例如Url.Action()通过路由生成的,而不是在控制器和视图中进行硬编码的,则对项目维护有很大帮助。

As long as your JavaScript is in an MVC view, you can just resolve Url.Action inline. 只要您的JavaScript在MVC视图中,您就可Url.Action联解析Url.Action

$(function () {

    var customersTable = $('#Customers');
    var returnCustomersTable = UpdateDataTable(customersTable, 
        '@Url.Action("Loaddata", "Customers")');
}

If the JavaScript is in a separate file outside of MVC, consider passing the URL through a variable or function parameter. 如果JavaScript在MVC之外的单独文件中,请考虑通过变量或函数参数传递URL。

This routing feature also makes it easier to deploy ASP.NET MVC, because the generated URLs will adapt to use the application path when the application is not deployed to the root website virtual directory. 此路由功能还使部署ASP.NET MVC更加容易,因为当未将应用程序部署到根网站虚拟目录时,生成的URL将适合使用应用程序路径。

Application running in IIS web site root: 在IIS网站根目录中运行的应用程序:

/Customers/Loaddata

Application running in virtual subdirectory configured as IIS Application named "MyAppName": 在配置为IIS应用程序的虚拟子目录中运行的应用程序名为“ MyAppName”:

/MyAppName/Customers/Loaddata

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

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