Hi folks,
inside my ASP.NET MVC controller, I've got a method that requires an HttpRequest
object. All I have access to is an HttpRequestBase
object.
Is there anyway I can somehow convert this?
What can/should I do??
Hi folks,
inside my ASP.NET MVC controller, I've got a method that requires an HttpRequest
object. All I have access to is an HttpRequestBase
object.
Is there anyway I can somehow convert this?
What can/should I do??
Does HttpRequest request = (HttpRequest) theobject;
work at all?
Typically when you need to access the HttpContext
property in a controller action, there is something you can do better design wise.
For example, if you need to access the current user, give your action method a parameter of type IPrincipal
, which you populate with an Attribute
and mock as you wish when testing. For a small example on how, see this blog post, and specifically point 7.
Is it your method, so you can re-write it to take HttpRequestBase
? If not, you can always get the current HttpRequest
from HttpContext.Current.HttpRequest
to pass on. However, I often wrap access to the HttpContext inside a class like mentioned in ASP.NET: Removing System.Web Dependencies for better unit testing support.
Hello,
There is no way to convert between these types.
We had a similar case. We rewrote our classes/web services methods so that they use HttpContextBase, HttpApplicationStateBase, HttpServerUtilityBase, HttpSessionStateBase... instead of the types of close name without the "Base" suffix (HttpContext, ... HttpSessionState). They are a lot easier to handle with home-made mocking.
I feel sorry you couldn't do it.