gpt4 book ai didi

c# - 405 方法不允许 Web API 2

转载 作者:太空狗 更新时间:2023-10-29 23:21:22 25 4
gpt4 key购买 nike

我已经浏览了几乎所有我能找到的关于 SO 和更多的文章。我对 Web.config 进行了更改以满足答案,因为它们似乎都指向删除 WebDAV 模块和处理程序。

但是,我仍然遇到错误:

405 Method Not Allowed

The requested resource does not support http method 'PUT'.

注意:这最初只是一个 MVC 4 项目。我添加了工件来支持 Web API。似乎我可能错过了一些东西。

注意 GET 调用在 Angular 中运行良好。

Web API 路由配置

configuration.Routes.MapHttpRoute(
name: "Default API",
routeTemplate: "api/{controller}/{id}",
defaults: new { action = "Get", id = RouteParameter.Optional },
constraints: new { id = @"[\da-z-]{36}" });

configuration.Routes.MapHttpRoute(
name: "Default API with Action",
routeTemplate: "api/{controller}/{action}",
defaults: new { action = "Get" });

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

Web.config

<modules runAllManagedModulesForAllRequests="true">
<remove name="WebDAVModule" />
</modules>
<handlers accessPolicy="Read, Execute, Script">
<remove name="WebDAV" />
<remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
<remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
<add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>

PUT API 入口

public async Task<IHttpActionResult> Put([FromBody] EditUserViewModel viewModel)

客户端

vm.model.$update(function () {
$state.go(userStates.app);
});

有没有可能是属于第二种路由配置?如果是这样,我如何验证 .NET 尝试使用的路由配置?

最佳答案

最后看来是我试图使路由表复杂化。我最终将它回滚到这个:

configuration.MapHttpAttributeRoutes();

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

请注意我还添加了 configuration.MapHttpAttributeRoutes();

信不信由你,在这两项更改之后,一切都运行良好。 Web.config 如下所示:

<handlers>
<remove name="ExtensionlessUrl-Integrated-4.0"/>
<add name="ExtensionlessUrl-Integrated-4.0"
path="*."
verb="*"
type="System.Web.Handlers.TransferRequestHandler"
preCondition="integratedMode,runtimeVersionv4.0"
responseBufferLimit="0" />
</handlers>

关于c# - 405 方法不允许 Web API 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34322523/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com