405方法不允许.Net Web Api

时间:2017-09-26 15:38:53

标签: .net angular asp.net-web-api xmlhttprequest

这个问题让我感到害怕,无论我怎么做,我似乎无法解决它。 我有一个Angular 4应用程序向.Net WEB Api发送Http请求。在IIS Express上(通过Visual Studio 2013),一切正常,包括Get,Put和Post方法。只要我在IIS服务器甚至本地IIS上配置Web Api,Get方法仍然有效,但Put和Post停止工作。

我的要求:

     headers.append('Content-Type', 'application/json');
     return this.http.put('http://iisserver/dbServerMonitor/api/server/updateServer', this.serversService.getServer(index), {headers: headers});

WebConfig.Cs

    public static void Register(HttpConfiguration config)
    {
        //Enable cors
        var cors = new EnableCorsAttribute("*", "accept, accesstoken, autorization, cache-control, pragma, content-type, origin", "GET, PUT, POST, DELETE, OPTIONS");
        // Web API configuration and services
        //var cors = new EnableCorsAttribute("*", "*", "*");
        //config.MessageHandlers.Add(new CorsHandler());

        config.EnableCors(cors);
        // Web API routes
        config.MapHttpAttributeRoutes();
        config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
    }

控制器:

    [HttpPut]
    [Route("updateServer")]
    public HttpResponseMessage updateServer([FromBody] Server server)
    {
      ....code

我也在控制器中:

    [HttpOptions]
    public HttpResponseMessage Options()
    {
        return new HttpResponseMessage { StatusCode = HttpStatusCode.OK };
    }

我尝试了一切,从更改标题,从请求代码中完全删除它们,处理web config xml文件中的CORS,但最终它只能在cs文件上运行。我完全不知道如何解决这个问题。

编辑:删除身份验证后,我现在可以成功调用Post和Put在我的本地IIS上。但是,在不同的IIS服务器(同一个域)上,我在Put和Post调用上获得了404。求助。

1 个答案:

答案 0 :(得分:-1)

您需要在IIS上启用所有动词。 IIS - 处理程序映射 - ExtensionlessUrlHandler-Integrated-4.0 - 请求限制... - 动词 - 以下动词之一或所有动词

enter image description here