【问题标题】:"No owin.Environment item was found in the context." when not in debug mode“没有在上下文中找到 owin.Environment 项。”不处于调试模式时
【发布时间】:2015-05-29 09:17:14
【问题描述】:

我对 Owin 及其上下文有一个奇怪的问题。我有一个看起来像这样的控制器操作

[HttpPost]
public ActionResult Login(string username, string password)
{
    var ctx = HttpContext.GetOwinContext();
    // code omitted
    return new HttpUnauthorizedResult();
}

如果我在var ctx 处设置断点并调试我的应用程序,则该变量将获得其值。但是如果只是正常启动应用程序并使用 IIS Express (ctrl+f5) 运行它,应用程序会在 var ctx 处中断并给我错误。

“在上下文中找不到 owin.Environment 项。”

是什么导致了这个错误?

我在 Visual Studio 2013 .Net 版本 4.5.1 中运行它。感谢您的帮助!

如果您认为需要更多来自 startup.cs 或其他任何地方的代码,请告诉我。

编辑:这是我的启动配置。

[assembly: OwinStartup(typeof(XXX.Startup))]
namespace XXX
{
    public partial class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            ConfigureAuth(app);
        }
    }
}

namespace XXX
{
    public partial class Startup
    {
        public void ConfigureAuth(IAppBuilder app)
        {
            app.UseCookieAuthentication(new CookieAuthenticationOptions
            {
                AuthenticationType = AuthenticationTypes.Cookie,
                AuthenticationMode = AuthenticationMode.Active,
                SlidingExpiration = true,
                ExpireTimeSpan = new TimeSpan(0, 30, 0),
            });

            app.UseMyAuthentication(new MyAuthenticationOptions()
            {
                AuthenticationType = AuthenticationTypes.MyAuthType,
                SignInAsAuthenticationType = AuthenticationTypes.Cookie,
                AuthenticationMode = AuthenticationMode.Passive,
                CallbackPath = new PathString("/authorization/callback")
            });

        }
    }
}

【问题讨论】:

  • this线程。
  • 添加了一些配置。 @YuvalItzchakov,它找到了启动类,但是在不执行 ctrl+f5 时获取上下文时会抛出异常,但在执行 f5 并通过 getOwinContext 方法调试时不会。

标签: c# asp.net-mvc owin katana


【解决方案1】:

看起来你不能多次“注册”*启动类,删除[assembly: OwinStartup(typeof(XXX.Startup))]使它在不调试时注册启动类。仍然不明白为什么它在调试时起作用。

*按照将 StartUp 类直接放在程序集下的约定,owin 反射代码会找到 StartUp 类。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-12
    • 1970-01-01
    相关资源
    最近更新 更多