Home > Net >  There is a public variable demand, I wrote a void, but each controller should add, is there a more c
There is a public variable demand, I wrote a void, but each controller should add, is there a more c

Time:05-05


The code is as follows,
 public IActionResult Index () 
{
GetAdminSession ();
Return the View ();
}
[the Route (" Singer ")]
Public IActionResult Singer ()
{
GetAdminSession ();
Return the View ();
}
[the Route (" Chord ")]
Public IActionResult Chord ()
{
GetAdminSession ();
Return the View ();
}
Public void GetAdminSession ()
{
If (HttpContext. Session. Get string (" AdminLevel ")!=null)
ViewBag. AdminLevel=HttpContext. Session. Get string (" AdminLevel ");
}

GetAdminSession (); For three times, is there a more concise way, I try to put in the constructor, found the HttpContext forever is empty, and error's plan to the constructor, HttpContext haven't generated

CodePudding user response:


HttpContext is the controller inside of a property, in the constructor, of course, it won't
Really want to, you can use dependency injection injection IHttpContextAccessor
Remember to register services. AddHttpContextAccessor ();

Ha, so injection in controller for httpcontext and seems redundant

CodePudding user response:

You this is under the framework of MVC or Asp.Net Core?
  •  Tags:  
  • C#
  • Related