405在WebAPI2上不允许方法(OWIN) [英] 405 Method Not Allowed on WebAPI2 (OWIN)
问题描述
我为我的API设置了一个WebAPI端点,但无法获取我的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和Angular已打开单独的网站。
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>
在Angular侧,我做一个简单的$ 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;
});
}
使用Postman进行调用时,我获得了成功的响应,同样的调用从Angular,我得到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示例网站发送相同的请求标头,但能够获取Access-Control-Allow-XXX标头
BitOfTech sample site, is sending the same request headers, but is able to get the Access-Control-Allow-XXX headers
>
我已更新我的代码以遵循基于令牌的身份验证使用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
推荐答案
我最近有类似的问题。我为飞行前的 OPTIONS
请求添加了控制器处理程序,它解决了这个问题。
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屋!