在我的生产代码中,我们遇到了 Request.GetOwinContext() 始终返回 null 的问题。
我设置了一个小型测试 WebAPI 控制器来尝试隔离问题:
public class TestController : ApiController
{
[HttpGet]
public async Task<IHttpActionResult> GetAsyncContext(string provider)
{
if (HttpContext.Current.GetOwinContext() == null)
return this.BadRequest("No HttpContext.Current Owin Context");
if (Request.GetOwinContext() == null)
return this.BadRequest("No Owin Context");
return this.Ok();
}
[HttpGet]
public IHttpActionResult GetContext(string provider)
{
if (HttpContext.Current.GetOwinContext() == null)
return this.BadRequest("No HttpContext.Current Owin Context");
if (Request.GetOwinContext() == null)
return this.BadRequest("No Owin Context");
return this.Ok();
}
}
起初我以为这可能与异步运行的action方法有关,但运行上面之后发现,在两个版本中,Request.GetOwinContext()都返回null。
我正在使用 Microsoft.AspNet.WebApi.Owin.5.1.1 (这似乎是定义 GetOwinContext() 扩展方法的地方)。
对这里发生的事情有什么想法吗???
另一个原因(尤其是从 ASP.NET MVC 4 和/或空 Web Api 模板升级后)是 WebAPI 项目根目录中缺少 Startup.cs 文件。
另外,请确保您已安装 Microsoft.Owin.Host.SystemWeb 包。
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Owin;
using Owin;
[assembly: OwinStartup(typeof(TestMVC5.Startup))]
namespace TestMVC5
{
public partial class Startup
{
public void Configuration(IAppBuilder app)
{
ConfigureAuth(app);
}
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)