405不允许的方法上WebAPI2(OWIN) [英] 405 Method Not Allowed on WebAPI2 (OWIN)

查看:446
本文介绍了405不允许的方法上WebAPI2(OWIN)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立的WebAPI端点我的API,但我有麻烦我AngularJS调用我的postRegister方法​​来工作。

I am setting up a WebAPI endpoint for my API, but am having trouble getting my AngularJS calls to my PostRegister method to work.

本的WebAPI和角度都在独立的站点。

The webAPI and Angular are on separate sites.

在Startup.cs我有:

On the Startup.cs I have:

    public void Configuration(IAppBuilder app)
    {
        ConfigureOAuth(app);

        var config = new HttpConfiguration();

        WebApiConfig.Register(config);
        app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
        app.UseWebApi(config);

    }

    private void ConfigureOAuth(IAppBuilder app)
    {
        var oAuthServerOptions = new OAuthAuthorizationServerOptions
        {
            AllowInsecureHttp = true,
            TokenEndpointPath = new PathString(Paths.TokenPath),
            AccessTokenExpireTimeSpan = TimeSpan.FromMinutes(30),
            Provider = Kernel.Get<IOAuthAuthorizationServerProvider>()
        };
        app.UseOAuthAuthorizationServer(oAuthServerOptions); 

        OAuthBearerOptions = new OAuthBearerAuthenticationOptions();
        app.UseOAuthBearerAuthentication(OAuthBearerOptions);
    }

然后在控制器上,我有我的方法,它被设置为允许匿名:

Then on the controller, I have my method that is set to allow anonymous:

    [AllowAnonymous]
    public bool PostRegister(UserSignupForm form)
    {
        ...
    }

我也更新了web.config中:

I've also updated the web.config:

 <system.webServer>
    <handlers>
      <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
      <remove name="OPTIONSVerbHandler" />
      <remove name="TRACEVerbHandler" />
      <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
    </handlers>
  </system.webServer>

在角边,我做一个简单的$ http.post电话:

On the Angular side, I make a simple $http.post call:

    saveRegistration: function (registration) {
        return $http.post(baseUrl + '/api/signup/register', registration).then(function (response) {
            return response;
        });
    }

使用邮差使得电话时,我得到一个成功的响应,但这样做的角一样打电话时,我得到的405错误。

When making the call using Postman, I get a successful response, but when doing the same call from Angular, I get the 405 error.

BitOfTech示例站点,发送相同的请求头,但是能够获得访问控制允许-XXX头

BitOfTech sample site, is sending the same request headers, but is able to get the Access-Control-Allow-XXX headers

我已经更新了我的code遵循<一个href=\"http://bitoftech.net/2014/06/01/token-based-authentication-asp-net-web-api-2-owin-asp-net-identity/\">Token使用ASP.NET的Web API基于2认证,Owin和身份

I've updated my code to follow Token Based Authentication using ASP.NET Web API 2, Owin, and Identity

推荐答案

最近,我有类似的问题。我添加控制器处理程序pre-飞行选项请求,并解决了这个问题。

I had similar issue recently. I added controller handler for the pre-flight OPTIONS request and it solved the problem.

[AllowAnonymous]
[Route("Register")]
[AcceptVerbs("OPTIONS")]
public async Task<IHttpActionResult> Register()
{
    return Ok();
}

这篇关于405不允许的方法上WebAPI2(OWIN)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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