gpt4 book ai didi

c# - Azure 移动应用程序 - 尝试 POST 时收到 405(不允许的方法)

转载 作者:行者123 更新时间:2023-12-03 00:43:43 25 4
gpt4 key购买 nike

我正在尝试将我的 Azure 移动服务 .NET 后端迁移到 Azure 移动应用程序。

我使用了一些自定义 Web Api Controller ,迁移后我得到了 405 (Method Not Allowed) /The requested resource does not support http method 'POST'.尝试 POST 到之前有效的 Controller 方法时出错。

我花了几个小时尝试不同的 CORS 设置,但到目前为止还没有成功。

这是我当前配置 Web Api 的方式:

HttpConfiguration config = new HttpConfiguration();

new MobileAppConfiguration()
.UseDefaultConfiguration()
.ApplyTo(config);

var cors = new EnableCorsAttribute("*", "*","*");
//var cors = new EnableCorsAttribute("*", "*","GET,POST,DELETE,HEAD,PUT,PATCH,OPTIONS");
config.EnableCors(cors);

config.Routes.MapHttpRoute(
name: "Rest",
routeTemplate: "rest/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);

config.MapHttpAttributeRoutes();

Controller 看起来像这样:

[Authorize]
[RoutePrefixAttribute("rest/companies")]
public class CompaniesController : ApiController
{
[HttpPost]
[Route("my-active")]
//[EnableCors("*","*","*")]
public HttpResponseMessage SetActive(/*[FromBody]*/Company company)
{
// Implementation
}
}

我也尝试过:

  • 在 web.config 中设置 CORS 设置(自定义 header /不同设置),例如。 <add name="Access-Control-Allow-Methods" value="GET,POST,DELETE,HEAD,PUT,PATCH,OPTIONS" />
  • 根据这篇博文添加了一个 cors 消息处理程序
    (http://blog.bittercoder.com/2012/09/09/cors-and-webapi/)
  • 此处理程序也被删除:<remove name="OPTIONSVerbHandler" />

我注意到的一件事是,Azure 移动应用程序组件似乎覆盖了我使用 config.EnableCors(cors) 配置的允许方法和允许 header 。 。我只能使用 web.config 和消息处理程序来控制所有设置。但无论如何也没有解决405问题。

目前,我不确定这是否是 CORS 问题。

有什么想法吗?目前很难找到关于移动应用程序的良好文档,如果 .NET 后端部分能够开源,我将不胜感激......这对我来说有点像一个黑匣子。

最佳答案

当您激活应用服务授权并忘记将移动客户端 URL 从 http 更改为 https 时,可能会发生这种情况。如果是这样,您的 http Post 将被重定向到 https url,但带有 Get 消息。感谢 Fiddler 找到了它。

关于c# - Azure 移动应用程序 - 尝试 POST 时收到 405(不允许的方法),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31630461/

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