简体   繁体   English

Angular JS在IIS上托管后未调用JsonResult

[英]Angular JS not calling JsonResult After Hosting on IIS

I am creating a Web App in .NetCore with Angular Js, Its working Fine in testing on localhost . 我正在使用Angular Js在.NetCore中创建一个Web应用程序,它在localhost上的测试中工作正常。 But when I host it in IIS server it failed to call a JsonResult Method 但是,当我将其托管在IIS服务器中时,它无法调用JsonResult方法

Below is my Code [Angular Js] 以下是我的代码[Angular Js]

         $http({
                url: '/Home/GetDetails',
                method: "GET",
                params: {
                    StaffCode: $scope.StaffCode,
                }
            }).then(function mySuccess(response) 
                {
                }, function myError(response) {
                $window.alert('An Error Occurred , Try Again Later');
            });

it is working fine on localhost , But after hosting on IIS server call to GetDetails Jsonresult is happening in wrong way 它在localhost上工作正常,但是在IIS服务器上托管后,对GetDetails Jsonresult的调用以错误的方式发生

For example : 例如 :

http://10.1.10.10/ApplicationName/Home/Index is the Web address of Application http://10.1.10.10/ApplicationName/Home/Index是应用程序的网址

And Call to GetDetails jsonresult is 并调用GetDetails jsonresult是

http://10.1.10.10/Home/GetDetails [Gives Error 404 : File Not Found] http://10.1.10.10/Home/GetDetails [提供错误404:找不到文件]

Instead of http://10.1.10.10/ApplicationName/Home/GetDetails 代替http://10.1.10.10/ApplicationName/Home/GetDetails

I tried 我试过了

url: '~/Home/GetDetails',

And

url: './Home/GetDetails',

But it does not seems to call right address of Jsonresult Method 但是它似乎没有调用Jsonresult方法的正确地址

What is the correct Format of url here ? url的正确格式是什么?

The problem here is you have hard-coded the urls which does not work if application is hosted inside some directory but not in root folder, so the solution is to always use Url.Action helper method to generate the correct relative urls like: 这里的问题是,您已经硬编码了URL,如果应用程序托管在某个目录中而不是根文件夹中,则该URL不起作用,因此解决方案是始终使用Url.Action帮助程序方法生成正确的相对URL,例如:

url: '@Url.Action("GetDetails","Home")'

This will make sure to generate the correct url regardless of application is hosted how much nested sub-directories. 这将确保生成正确的url,而不管应用程序托管了多少嵌套子目录。

Hope it helps! 希望能帮助到你!

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

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