Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I convert an HttpRequestBase into an HttpRequest object?

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??

like image 777
Pure.Krome Avatar asked Sep 21 '09 01:09

Pure.Krome


3 Answers

You should always use HttpRequestBase and HttpResponseBase in your application as opposed to the concrete versions which are impossible to test (without typemock or some other magic).

Simply use the HttpRequestWrapper class to convert as shown below.

var httpRequestBase = new HttpRequestWrapper(Context.Request);
like image 65
CountZero Avatar answered Nov 05 '22 05:11

CountZero


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.

like image 24
Kevin Hakanson Avatar answered Nov 05 '22 05:11

Kevin Hakanson


You can just use

System.Web.HttpContext.Current.Request

The key here is that you need the full namespace to get to the "correct" HttpContext.

I know it's been 4 years since this question was asked, but if this will help somebody, then here you go!

(Edit: I see that Kevin Hakanson already gave this answer...so hopefully my response will help those people who just read answers and not comments.) :)

like image 34
adamgede Avatar answered Nov 05 '22 04:11

adamgede