相关疑难解决方法(0)

Web API Put Request生成Http 405 Method Not Allowed错误

这是PUT我的Web API上方法的调用- 方法的第三行(我从ASP.NET MVC前端调用Web API):

在此输入图像描述

client.BaseAddresshttp://localhost/CallCOPAPI/.

这是contactUri:

在此输入图像描述

这是contactUri.PathAndQuery:

在此输入图像描述

最后,这是我的405回复:

在此输入图像描述

这是我的Web API项目中的WebApi.config:

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

            config.Routes.MapHttpRoute(
                name: "DefaultApiGet",
                routeTemplate: "api/{controller}/{action}/{regionId}",
                defaults: new { action = "Get" },
                constraints: new { httpMethod = new HttpMethodConstraint("GET") });

            var json = config.Formatters.JsonFormatter;
            json.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects;
            config.Formatters.Remove(config.Formatters.XmlFormatter);
Run Code Online (Sandbox Code Playgroud)

我试着剥离下来获取传递到路径PutAsJsonAsync,以string.Format("/api/department/{0}", department.Id)string.Format("http://localhost/CallCOPAPI/api/department/{0}", department.Id)没有运气.

有没有人有任何想法为什么我得到405错误?

UPDATE

根据请求,这是我的部门控制器代码(我将发布我的前端项目的部门控制器代码,以及WebAPI的部门ApiController代码):

前端部门控制员 …

c# asp.net-mvc json http-status-code-405 asp.net-web-api

125
推荐指数
5
解决办法
15万
查看次数