I’ve been working with ASP.NET(WebForm) for a while, but new to ASP.NET MVC. From many articles I’ve read, in most cases the reason that the controllers are hard to test is because they are accessing the runtime components: HttpContext (including Request, Response …). Accessing HttpContext in a controller seems bad.
However, I must access these components somewhere, reading input from Request, sending results back via Response, and using Session to hold a few state variables.
So where is the best place to access these runtime components if we don’t access them in a controller?
when you call a model method in your controller, the Request and Response objects are carrying the same value or outputting to the same source. the “page” is what matters there for these objects.
And one more thing, the
Request,SessionandResponseobjects may not be directly referenced in your models so you can useSystem.Web.HttpContext.Currentto get the objects. They will function the same as its called from the controller.And Controllers meant to be as a bridge between views and models, and models should work even if there isn’t any Response or Request value in these objects, so I would use these objects’ values as usual parameters to model methods instead of referencing them inside the model. It’s the right usage of the MVC concept.