HTTP 405 Errors after Publishing Web API 2 Applica

2019-07-22 14:13发布

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);

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: '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 }}

And

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

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();
    }
}

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>

I use this webapi2 application in my winform application.

3条回答
Deceive 欺骗
2楼-- · 2019-07-22 14:54

It is the http error code who tell us that it is a right problem. Have a look at : https://fr.wikipedia.org/wiki/Liste_des_codes_HTTP

查看更多
手持菜刀,她持情操
3楼-- · 2019-07-22 14:56

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

Preferred Domain Setting

查看更多
爱情/是我丢掉的垃圾
4楼-- · 2019-07-22 15:02

You have a problem of right on the web server. 405 http code means that the post method is not allowed to use from your web server. Make you sure that you are authenticated when you send request and also make you sure that your request contain some token for authentication.

查看更多
登录 后发表回答