ASP.Net WebAPI OWIN:为什么Request.GetOwinContext()返回null? [英] ASP.Net WebAPI OWIN: Why would Request.GetOwinContext() return null?

查看:83
本文介绍了ASP.Net WebAPI OWIN:为什么Request.GetOwinContext()返回null?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的生产代码中,我们遇到了一个问题,其中Request.GetOwinContext()始终返回null.

In my production code we're having a problem where Request.GetOwinContext() always returns null.

我设置了一个小型测试WebAPI控制器来尝试找出问题所在:

I setup a small test WebAPI controller to try and isolate the problem:

    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.

At first I thought it might have something to do with the action method running asynchronously, but after running the above, it turns out that in both versions, Request.GetOwinContext() returns null.

我正在使用Microsoft.AspNet.WebApi.Owin.5.1.1(似乎在其中定义了GetOwinContext()扩展方法的地方).

I am using Microsoft.AspNet.WebApi.Owin.5.1.1 (which is where it seems the GetOwinContext() extension method is defined).

关于这里发生的事情有什么想法吗?

Any ideas on what's happening here???

推荐答案

另一个原因(尤其是在升级ASP.NET MVC4和/或空WebApi模板之后)在WebAPI项目的根目录中缺少Startup.cs文件.

Another cause (especially after upgrading form ASP.NET MVC4 and / or Empty WebApi Template) is missing Startup.cs file in the root of WebAPI project.

此外,请确保已安装Microsoft.Owin.Host.SystemWeb软件包.

Also, make sure that you have installed Microsoft.Owin.Host.SystemWeb package.

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);
        }
    }
}

这篇关于ASP.Net WebAPI OWIN:为什么Request.GetOwinContext()返回null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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