【问题标题】:Web API 404 not found after deploy asp net core web application部署 ASP Net Core Web 应用程序后未找到 Web API 404
【发布时间】:2017-11-12 02:53:36
【问题描述】:

我使用 vs 2017 创建一个包含两个项目的解决方案:

  • 1- 网络接口
  • 2- 通过 Angular 服务访问 WebApi 控制器的 Asp .Net Core Web 应用程序。

我有一个 Web Api 控制器

[Route("api/[controller]/[action]")]
public class ClientController : Controller
{
    // GET: api/Client/Get
    [HttpGet]
    public JsonResult Get()
    {
        ClientQuery _Query = new ClientQuery();
        List<ClientLisModel> _Results = _Query.GetAll();

        return new JsonResult(_Results);
    }

在我的角度服务中,我创建了一个函数来访问这个控制器操作:

this.getListeClient = function getListeClient() {
             var deferred = $q.defer();

                $http({
                    method: 'GET',
                    url: '/api/Client/Get'
                })
                    .then(function (data) {
                        deferred.resolve(data);
                    })
                    .catch(function (msg, code) {
                        deferred.reject(msg);
                        $log.error(msg, code);
                    });

                return deferred.promise;
            }

当我从 vs 2017 运行我的应用程序时,一切正常。但是当我在 iis 7.5 中将它部署到 MyWebSite 时。我有这个错误:

GET http://localhost/api/Client/Get 404(未找到)

它应该寻找http://localhost/MyWebSite/api/Client/Get

我的 Web.config 是:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
    <handlers>
      <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
    </handlers>
    <aspNetCore processPath="dotnet" arguments=".\Application.UI.Web.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" />
  </system.webServer>
</configuration>

谢谢!

【问题讨论】:

    标签: angularjs asp.net-web-api asp.net-core-1.1


    【解决方案1】:

    这个问题的解决方法是在一个模块中使用config。

    所以在我的 Angular js 模块中,我添加了这一行:

    (function () {
        'use strict';
    
        angular.module('clientApp', ['ui.bootstrap', 'smart-table']);
    
        angular.module('clientApp').constant('config', {
            apiUrl: 'http://localhost:55555',
            baseUrl: '/',
            enableDebug: true
        });
    })();
    

    我像这样修改我的 Angular js 服务:

    angular
            .module('clientApp')
            .service("clientService", function (config, $http, $q) {
                this.getListeClient = function getListeClient() {
                    var deferred = $q.defer();
    
                    $http({
                        method: 'GET',
                        url: config.apiUrl + '/api/Client/Get'
                    })
                        .then(function (data) {
                            deferred.resolve(data);
                        })
                        .catch(function (msg, code) {
                            deferred.reject(msg);
                            $log.error(msg, code);
                        });
    
                    return deferred.promise;
                }
    

    发布我的应用程序后,我只需修改 mu angularjs 模块中的“apiUrl”。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-30
      • 2012-11-14
      • 2016-08-20
      • 1970-01-01
      • 2021-10-31
      • 1970-01-01
      相关资源
      最近更新 更多