【问题标题】:Issue after deploying ASP.NET Web API to production将 ASP.NET Web API 部署到生产环境后的问题
【发布时间】:2020-05-16 18:02:07
【问题描述】:

我在本地环境中使用 IIS Express 和其他具有完整 IIS 的服务器上测试了 ASP.NET Web API。在这两个地方,token api 和 post api 都运行良好。我按照建议使用 System.Web.Http 命名空间。但是,将其部署到共享托管服务器后,出现以下错误:

令牌API:

"error": "unsupported_grant_type"

Post API(System.Web.Http的[HttpPost]方法):

"Message": "The requested resource does not support http method 'GET'."

发布 API 代码:

[AllowAnonymous]
[HttpPost]
[Route("api/test")]
public IHttpActionResult Test([FromBody] Employee emp)
{
    ...
}

我正在使用 WebApiConfig.cs 中提供的默认路由,如下所示。不确定这是否重要,因为我已经在使用 Route 装饰 API 方法:

config.Routes.MapHttpRoute(
    name: "DefaultApi",
    routeTemplate: "api/{controller}/{id}",
    defaults: new { id = RouteParameter.Optional }
);

我尝试使用 FileSystem 方法(将文件上传到服务器并在 IIS 中配置应用程序)和 Web Deploy 方法进行部署以直接将 API 部署到服务器。我正在使用 Postman 进行测试,并使用与本地测试时相同的 Http Method (Post) 和 body。

导致此问题的本地 IIS Express/IIS 和共享托管服务器 IIS 之间可能有什么区别?

【问题讨论】:

  • 错误消息表明您正在使用 GET 请求访问 POST 端点。

标签: asp.net iis asp.net-web-api asp.net-web-api-routing


【解决方案1】:

据我所知,"error": "unsupported_grant_type" 表示您在请求中使用了错误的参数,建议您重新检查您的请求标头或正文,以确保您使用了正确的参数类型。

"Message": "The requested resource does not support http method 'GET'." 错误表示您使用 get 方法而不是 post 来发送请求。

如果你真的使用post,我建议你可以使用Fail request tracking来看看你使用postman发送请求时发生了什么。 Article.

`

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-08-27
    • 2017-02-01
    • 1970-01-01
    • 2018-11-26
    • 2012-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多