'System.Web.HttpContext'不包含'GetOwinContext'Mystery的定义

时间:2021-09-11 16:57:12

I realize this question might seem trivial to some, but it's these types of things that I find myself fighting with quite a bit and I just want to make sense of it all despite that seeming to be a losing battle in .net (for me anyway).

我意识到这个问题对某些人来说可能看起来微不足道,但是这些类型的东西让我发现自己很多时候都在争吵,我只是想弄清楚这一切尽管在.net中似乎是一场失败的战斗(无论如何对我来说) )。

So, if I do the following:

所以,如果我做以下事情:

    using System.Web;
...
ApplicationUser user = System.Web.HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>().FindById(System.Web.HttpContext.User.Identity.GetUserId());

That produces the error in the title and a red GetOwinContext() and the error Cannot resolve symbol 'GetOwinContext()'

这会产生标题中的错误和一个红色的GetOwinContext()和错误无法解析符号'GetOwinContext()'

However, if I do the following (remove System.Web from in front of HttpContext), it works as expected (or at least no errors):

但是,如果我执行以下操作(从HttpContext前面删除System.Web),它按预期工作(或至少没有错误):

using System.Web;
    ...
    ApplicationUser user = HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>().FindById(System.Web.HttpContext.User.Identity.GetUserId());

However, if I do this (same line that's working with using System.Web commented out):

但是,如果我这样做(使用System.Web注释掉的同一行):

    //using System.Web;
            ...
ApplicationUser user = HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>().FindById(System.Web.HttpContext.User.Identity.GetUserId());

It produces the same red GetOwinContext() and the error Cannot resolve symbol 'GetOwinContext()'

它产生相同的红色GetOwinContext()并且错误无法解析符号'GetOwinContext()'

If I google HttpContext I can only find that it stems from System.Web.

如果我谷歌HttpContext我只能发现它源于System.Web。

So, the question is why can't I use the full syntax like in the first example above? (Also mentioned in the answer here with the highest votes: ASP.NET MVC 5 - Identity. How to get current ApplicationUser)

所以,问题是为什么我不能像上面第一个例子那样使用完整的语法? (在答案中也提到了最高票数:ASP.NET MVC 5 - Identity。如何获取当前的ApplicationUser)

UPDATE (to address duplicate question reply): While there is an answer on that question that may come to the same conclusion, I don't really understand how this is a duplicate question. Try to think of it from a newbie perspective and dissecting all the smoke and mirrors that is .Net. I have never tried to learn something so convoluted in my life as .Net and sometimes you have to look at things from many different angles.

更新(以解决重复的问题回复):虽然有一个问题的答案可能得出相同的结论,但我真的不明白这是一个重复的问题。试着从新手的角度来思考它,并解析所有的烟雾和镜子.Net。我从来没有尝试过像生活中那样令人费解的东西。有时你必须从许多不同的角度看问题。

I actually saw that question and one other regarding using Current, but neither struck me as 1) being the answer I was looking for (at the time) 2) more importantly, why it's behaving like that. Sam's answer is perfect, although a bit over my head. But, at least now, I can go research what it all means...

我实际上看到了这个问题和另一个关于使用Current的问题,但是没有让我感到震惊1)作为我正在寻找的答案(当时)2)更重要的是,为什么它表现得那样。 Sam的回答很完美,虽然有点过头了。但是,至少现在,我可以研究这一切意味着什么......

4 个解决方案

#1


20  

When you are writing System.Web.HttpContext actually you are pointing to a class. But when you are writing HttpContext inside of a controller you are using a property named HttpContext which return an object of HttpContext class. You could also reach the same object by calling System.Web.HttpContext.Current static property. Therefore you could write:

实际上,当您编写System.Web.HttpContext时,您指向的是一个类。但是当您在控制器内部编写HttpContext时,您正在使用名为HttpContext的属性,该属性返回HttpContext类的对象。您还可以通过调用System.Web.HttpContext.Current静态属性来访问同一个对象。因此你可以写:

System.Web.HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>();

#2


6  

DSR's answer in this link worked for me

DSR在这个链接中的答案对我有用

Working in MVC 5 Identity for email verification

使用MVC 5 Identity进行电子邮件验证

It seems you have missing NuGet package called 'Microsoft.Owin.Host.SystemWeb'. Install package from NuGet or use package manager console to install the package.

您似乎缺少名为“Microsoft.Owin.Host.SystemWeb”的NuGet包。从NuGet安装包或使用包管理器控制台安装包。

Install-Package Microsoft.Owin.Host.SystemWeb

安装包Microsoft.Owin.Host.SystemWeb

Hope this helps.

希望这可以帮助。

#3


3  

Just go to NuGet Packages Manager of the project, search and install this Microsoft.Owin.Host.SystemWeb and you can use using System.Web; to enable GetOwinContext() method

只需转到该项目的NuGet Packages Manager,搜索并安装此Microsoft.Owin.Host.SystemWeb即可使用System.Web;启用GetOwinContext()方法

#4


1  

Thanks it works in my case when i added Microsoft.Owin.Host.SystemWeb from NugetpackageManager. It provides the extension method GetOwinContext() to the HttpContext.

当我从NugetpackageManager添加Microsoft.Owin.Host.SystemWeb时,感谢它适用于我的情况。它为HttpContext提供了扩展方法GetOwinContext()。

#1


20  

When you are writing System.Web.HttpContext actually you are pointing to a class. But when you are writing HttpContext inside of a controller you are using a property named HttpContext which return an object of HttpContext class. You could also reach the same object by calling System.Web.HttpContext.Current static property. Therefore you could write:

实际上,当您编写System.Web.HttpContext时,您指向的是一个类。但是当您在控制器内部编写HttpContext时,您正在使用名为HttpContext的属性,该属性返回HttpContext类的对象。您还可以通过调用System.Web.HttpContext.Current静态属性来访问同一个对象。因此你可以写:

System.Web.HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>();

#2


6  

DSR's answer in this link worked for me

DSR在这个链接中的答案对我有用

Working in MVC 5 Identity for email verification

使用MVC 5 Identity进行电子邮件验证

It seems you have missing NuGet package called 'Microsoft.Owin.Host.SystemWeb'. Install package from NuGet or use package manager console to install the package.

您似乎缺少名为“Microsoft.Owin.Host.SystemWeb”的NuGet包。从NuGet安装包或使用包管理器控制台安装包。

Install-Package Microsoft.Owin.Host.SystemWeb

安装包Microsoft.Owin.Host.SystemWeb

Hope this helps.

希望这可以帮助。

#3


3  

Just go to NuGet Packages Manager of the project, search and install this Microsoft.Owin.Host.SystemWeb and you can use using System.Web; to enable GetOwinContext() method

只需转到该项目的NuGet Packages Manager,搜索并安装此Microsoft.Owin.Host.SystemWeb即可使用System.Web;启用GetOwinContext()方法

#4


1  

Thanks it works in my case when i added Microsoft.Owin.Host.SystemWeb from NugetpackageManager. It provides the extension method GetOwinContext() to the HttpContext.

当我从NugetpackageManager添加Microsoft.Owin.Host.SystemWeb时,感谢它适用于我的情况。它为HttpContext提供了扩展方法GetOwinContext()。