Azure移动应用程序-尝试POST时获取405(不允许使用方法) [英] Azure Mobile App - Getting 405 (Method Not Allowed) when trying POST

查看:120
本文介绍了Azure移动应用程序-尝试POST时获取405(不允许使用方法)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to migrate my Azure Mobile Service .NET backend to an Azure Mobile App.

我使用了一些自定义的Web Api控制器,并且在迁移后,尝试发布到以前可以使用的控制器方法时,出现了 405 (Method Not Allowed) /The requested resource does not support http method 'POST'.错误.

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'. error when trying to POST to a controller method that worked before.

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

I spent hours trying diffent CORS settings but I had no success so far.

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

This is how I currently configure 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();

控制器看起来像这样:

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

我也尝试过:

  • 在web.config中设置CORS设置(自定义标题/其他设置),例如<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" />
  • Set CORS settings in web.config (custom headers / different settings), eg. <add name="Access-Control-Allow-Methods" value="GET,POST,DELETE,HEAD,PUT,PATCH,OPTIONS" />
  • Added a cors message handler according this blog post
    (http://blog.bittercoder.com/2012/09/09/cors-and-webapi/)
  • This handler is also removed: <remove name="OPTIONSVerbHandler" />

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

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). I was only able to control all settings using web.config and the message handler. But it did not solve the 405 problem anyway.

目前,我不确定这是否完全是CORS问题.

At this point, I'm not sure if it's a CORS problem at all.

有什么想法吗?目前很难在Mobile Apps上找到好的文档,如果.NET后端部分是开源的,我将不胜感激.对于我来说,这似乎是一个黑匣子.

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.

推荐答案

OMG,我发现我的代码有问题.我不得不交换这两个语句:

OMG, I found the problem with my code. 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 }
);

当我使用Azure移动服务时,调用MapHttpAttributeRoutes导致错误已添加具有相同键的项",因此我删除了该行.为了使属性路由正常工作,我不得不再次将其重新插入到Azure移动应用程序中,但是我在错误的位置进行了操作,因此请小心.

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. 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移动应用程序-尝试POST时获取405(不允许使用方法)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆