C# &引用;在上下文中未找到owin.Environment项。”;不处于调试模式时

C# &引用;在上下文中未找到owin.Environment项。”;不处于调试模式时,c#,asp.net-mvc,owin,katana,C#,Asp.net Mvc,Owin,Katana,关于Owin及其背景,我有一个奇怪的问题。我有一个控制器动作,看起来像这样 [HttpPost] public ActionResult Login(string username, string password) { var ctx = HttpContext.GetOwinContext(); // code omitted return new HttpUnauthorizedResult(); } 如果我在var ctx设置断点并调试我的应用程序,变量将获得其

关于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")
            });

        }
    }
}
看起来您不能多次“注册”*启动类,删除
[assembly:OwinStartup(typeof(XXX.startup))]
使其在不调试时注册启动类。但我还是不明白为什么在调试时它会起作用


*按照将启动类直接放在程序集下的惯例,owin反射代码将查找启动类。

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