Difference between HttpContext.Current and Controller.Context in MVC ASP.NET

后端 未结 2 1442
南笙
南笙 2020-12-07 14:40

I am working on an MVC ASP .NET application. I am relatively new to both.

In a controller I am trying to get the current log on user, for which there seem to be two

2条回答
  •  醉酒成梦
    2020-12-07 15:17

    The context provided by the controller (not the static HttpContext.Current) is mockable. If you're interested in unit-testing your code, it's generally far easier to create a mock ControllerContext and set it on the Controller than it is to go through HttpContext.Current. Otherwise ControllerContext.HttpContext points to the same data as HttpContext.Current.

提交回复
热议问题