本文介绍了HttpContext.Current.User在ControllerBase(asp.net mvc)中为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在ASP.NET MVC应用程序中有一个ControllerBase
类.其他控制器继承自ControllerBase
.
I have a ControllerBase
class in an ASP.NET MVC Application. The other controllers inherit from ControllerBase
.
我想访问HttpContext.User.Identity.Name
,但是HttpContext
是null
.怎么了?
I want to access HttpContext.User.Identity.Name
, but HttpContext
is null
. What's the matter?
public ControllerBase()
{
var dataManager=new DataManager();
if (HttpContext.User.Identity.IsAuthenticated) // throws error
{
ViewData["assets"] = ud.BalanceFreeze + ud.Balance + ud.BalanceRealty;
ViewData["onaccount"] = ud.Balance;
ViewData["pending"] = ud.BalanceFreeze;
ViewData["inrealty"] = ud.BalanceRealty;
}
推荐答案
尝试将代码添加到ControllerBase中的此事件中:
Try adding your code to this event in your ControllerBase:
protected override void Initialize(RequestContext requestContext){
}
这篇关于HttpContext.Current.User在ControllerBase(asp.net mvc)中为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!