简体   繁体   English

Azure 移动应用程序 - 尝试 POST 时出现 405(不允许的方法)

[英]Azure Mobile App - Getting 405 (Method Not Allowed) when trying POST

I'm trying to migrate my Azure Mobile Service .NET backend to an Azure Mobile App.我正在尝试将我的 Azure 移动服务 .NET 后端迁移到 Azure 移动应用程序。

I was using some custom Web Api controllers, and after migration I'm getting a 405 (Method Not Allowed) / The requested resource does not support http method 'POST'.我正在使用一些自定义 Web Api 控制器,并且在迁移后我得到405 (Method Not Allowed) / The requested resource does not support http method 'POST'. error when trying to POST to a controller method that worked before.尝试 POST 到以前工作的控制器方法时出错。

I spent hours trying diffent CORS settings but I had no success so far.我花了几个小时尝试不同的 CORS 设置,但到目前为止我没有成功。

This is how I currently configure Web Api:这是我目前配置 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();

The controller looks like that:控制器看起来像这样:

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

What I tried too:我也尝试过:

  • Set CORS settings in web.config (custom headers / different settings), eg.在 web.config 中设置 CORS 设置(自定义标题/不同设置),例如。 <add name="Access-Control-Allow-Methods" value="GET,POST,DELETE,HEAD,PUT,PATCH,OPTIONS" />
  • Added a cors message handler according this blog post根据此博客文章添加了 cors 消息处理程序
    ( http://blog.bittercoder.com/2012/09/09/cors-and-webapi/ ) http://blog.bittercoder.com/2012/09/09/cors-and-webapi/
  • This handler is also removed: <remove name="OPTIONSVerbHandler" />此处理程序也被删除: <remove name="OPTIONSVerbHandler" />

One thing I noticed is, that a Azure Mobile App component seems to override the allowed methods and allowed headers that I configured using config.EnableCors(cors) .我注意到的一件事是,Azure 移动应用程序组件似乎覆盖了我使用config.EnableCors(cors)配置的允许方法和允许的标头。 I was only able to control all settings using web.config and the message handler.我只能使用 web.config 和消息处理程序控制所有设置。 But it did not solve the 405 problem anyway.但无论如何它并没有解决405问题。

At this point, I'm not sure if it's a CORS problem at all.在这一点上,我不确定这是否是 CORS 问题。

Any ideas?有任何想法吗? It's currently hard to find good documentation on Mobile Apps and I would appreciate if the .NET backend part would be open sourced... It's somewhat of a black box for me.目前很难找到关于移动应用程序的好的文档,如果 .NET 后端部分是开源的,我将不胜感激......对我来说这有点像一个黑盒子。

It could happen when you activate App Service Authorization and forget to change your mobile client url from http to https.当您激活应用服务授权并忘记将您的移动客户端 url 从 http 更改为 https 时,可能会发生这种情况。 If so, your http Post will be redirected to the https url but with a Get message.如果是这样,您的 http Post 将被重定向到 https url,但带有 Get 消息。 Found it thanks to Fiddler.感谢 Fiddler 找到它。

OMG, I found the problem with my code. OMG,我发现我的代码有问题。 I had to swap this two statements:我不得不交换这两个语句:

// Needs to be called before MapHttpRoute
config.MapHttpAttributeRoutes();

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

As I was using Azure Mobile Services, calling MapHttpAttributeRoutes caused an error 'An item with the same key has already been added', so I removed that line.当我使用 Azure 移动服务时,调用MapHttpAttributeRoutes导致错误“已添加具有相同密钥的项目”,因此我删除了该行。 I had to re-insert it for Azure Mobile Apps again in order to get attribute routing to work, but I did it at the wrong place, so be careful.我不得不再次为 Azure 移动应用程序重新插入它以使属性路由工作,但我在错误的地方做的,所以要小心。

If http Post is redirected to the https url as Get, try calling https directly.如果 http Post 作为 Get 重定向到 https url,请尝试直接调用 https。

Azure logs looks as follows in this case:在这种情况下,Azure 日志如下所示:

Received request: POST http://xxx.azurewebsites.net/api/Data/test
Information Redirecting: https://xxx.azurewebsites.net/api/Data/test
Received request: GET https://xxx.azurewebsites.net/api/Data/test

in this case call https ://xxx.azurewebsites.net/api/Data/test在这种情况下调用https://xxx.azurewebsites.net/api/Data/test

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM