发布Web API 2应用程序后的HTTP 405错误 [英] HTTP 405 Errors after Publishing Web API 2 Applications

查看:214
本文介绍了发布Web API 2应用程序后的HTTP 405错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Web Api控制器,并且已经在localhost上测试了所有操作,并且运行良好.但是当我在Web服务器上发布它时,仅[HttpGet]的操作有效,而[HttpPost]的操作返回Http 405错误

I have a Web Api Controller and i have tested all actions on localhost and it works well.But when i Published it on Web Server,Just Actions with [HttpGet] works and [HttpPost] Actions return Http 405 error

public class ContentController : ApiController
{
    [HttpGet]
    public async Task<IHttpActionResult> AdvantageList()
    {
        //return ok 200
    }
    [HttpPost]
    public async Task<IHttpActionResult> SaveAdvantage(ContentModel model)
    {
        //return 405 
    }
}

我在客户端上使用了以下方法

I used below method on client

var r = await ClientManager.Client.PostAsJsonAsync("api/Content/SaveAdvantage", Advantage);

但是它将在响应表单服务器下重新运行.我使用了PostAsJsonAsync方法,但是它说The requested resource does not support http method 'GET' 有谁知道为什么吗?

But it will retrun below response form server.I Used PostAsJsonAsync method but it says that The requested resource does not support http method 'GET' Does any one know why?

{ StatusCode:405,ReasonPhrase:不允许使用方法",版本:1.0,内容:System.Net.Http.StreamContent,标头: { 语法:无快取 X-Powered-By-Plesk:PleskWin 连接方式:关闭 缓存控制:无缓存 日期:2017年9月29日,星期五,格林尼治标准时间 伺服器:Microsoft-IIS/8.0 X-AspNet版本:4.0.30319 X-Powered-by:ASP.NET 内容长度:72 允许:POST 内容类型:application/json;字符集= utf-8 过期:-1 }}

{ StatusCode: 405, ReasonPhrase: 'Method Not Allowed', Version: 1.0, Content: System.Net.Http.StreamContent, Headers: { Pragma: no-cache X-Powered-By-Plesk: PleskWin Connection: close Cache-Control: no-cache Date: Fri, 29 Sep 2017 08:53:51 GMT Server: Microsoft-IIS/8.0 X-AspNet-Version: 4.0.30319 X-Powered-By: ASP.NET Content-Length: 72 Allow: POST Content-Type: application/json; charset=utf-8 Expires: -1 }}

还有

"{\"消息\:\"请求的资源不支持http方法'GET'.\}"

"{\"message\":\"The requested resource does not support http method 'GET'.\"}"

我的Web api配置中包含以下内容:

I have the below in my web api config:

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        config.Routes.MapHttpRoute("DefaultApi", "api/{controller}/{action}");
        config.Routes.MapHttpRoute("WithId", "api/{controller}/{action}/{id}", new { id = RouteParameter.Optional });
        config.Routes.MapHttpRoute("TwoId", "api/{controller}/{action}/{id}/{id2}", new { id = RouteParameter.Optional, id2 = RouteParameter.Optional });

        config.MapHttpAttributeRoutes();

        var formatter = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
        formatter.SerializerSettings.ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver();
    }
}

我在Web.config中有以下处理程序

I have below handlers in Web.config

<system.webServer>
<handlers>
  <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
  <remove name="OPTIONSVerbHandler" />
  <remove name="TRACEVerbHandler" />
  <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
</system.webServer>

我在winform应用程序中使用了这个webapi2应用程序.

I use this webapi2 application in my winform application.

推荐答案

在我的Web服务器上的托管设置中,有一个属性Preferred domain,我将其更改为 none ,然后它正常工作了

In hosting Setting in my web server ,had a property Preferred domain ,i changed it to none and after that it worked properly

这篇关于发布Web API 2应用程序后的HTTP 405错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆