Home > Net >  The core asp.net MVC the HttpContext object
The core asp.net MVC the HttpContext object

Time:05-11

Because the Controller inherits the Controller:
Public class HomeController: Controller
{
}

So, you can use in the HomeController HttpContext, however, the controller application often to new other classes, and other classes is to use some of the HttpContext object function

For example:
Internal class myWorkClass () {
Internal void myMothed () {
Var s=HttpContext. Request. The Path;
}
}
Because myWorkClass not inherit in the Controller, so there is no HttpContext object,

Of course, can be in HomeController calls the HttpContext as myMothed parameters or the structure of the myWorkClass parameters,
But, in my program, there are a lot of classes need to use to the contents of the HttpContext (not only the Request. The Path, and the Session, etc.)
Is there a more convenient method of:?

CodePudding user response:

You have a design problem, now generally at Asp.Net the Core code, basic not new controller inside an object, but by the Ioc container injection;

If your MyWorkClass is a service class, you should be in through the Ioc container injection, and it should not be used httpcontext

And if you MyWorkClass is deal with some common logic, you should consider using the Filter, or consider AOP.

CodePudding user response:

Generally need to deal with the httpcontext, also can consider middleware
  • Related