How do I convert an HttpRequest into an HttpRequestBase object?

asp.net MvcHttprequest

asp.net Mvc Problem Overview


My problem is the opposite of this: How do I convert an HttpRequestBase into an HttpRequest object?

In my ASP.NET MVC application I have a method used by many controllers that receive an HttpRequestBase as argument.

Now I have to call that method from another method, that is not an action (it's an nhibernate interceptor). In this second method I could access HttpContext.Current.Request, that is a HttpRequest and I cannot cast it to HttpRequestBase (I thought it was possibile due to the naming ...).

Does someone know in what relationship are this classes and how can I solve my problem? Thank you.

asp.net Mvc Solutions


Solution 1 - asp.net Mvc

You'll want to wrap your HttpRequest in a HttpRequestWrapper:

var wrapper = new HttpRequestWrapper(httpRequest);

The HttpRequestWrapper inherits from HttpRequestBase.

Solution 2 - asp.net Mvc

This is an another solution which does not require to create a new instance:

var httpRequestBase = myHttpRequest.RequestContext.HttpContext.Request;

Solution 3 - asp.net Mvc

In my application I had calls coming from several different places that needed access to the HttpRequestBase. I created this set of extension methods to get and convert from several different Http types into a HttpRequestBase, then used HttpRequestBase as the base type for interface and class methods through the application when I needed access to the request.

public static class RequestExtensions
{
    public static HttpRequestBase GetHttpRequestBase(this HttpContext httpContext)
    {
        if (httpContext == null)
        {
            throw new ArgumentException("Context is null.");
        }

        return httpContext.Request.ToHttpRequestBase();
    }

    public static HttpRequestBase GetHttpRequestBase(this HttpRequestMessage httpRequestMessage)
    {
        if (httpRequestMessage == null)
        {
            throw new ArgumentException("Request message is null.");
        }

        HttpContextWrapper context = null;
        if (httpRequestMessage.Properties.ContainsKey("MS_HttpContext"))
        {
            context = httpRequestMessage.Properties["MS_HttpContext"] as HttpContextWrapper;
        }
        if (context == null)
        {
            throw new ArgumentException("Context is null.");
        }

        return context.Request;
    }

    public static HttpRequestBase GetHttpRequestBase(this HttpApplication httpApplication)
    {
        if (httpApplication == null)
        {
            throw new ArgumentException("Application is null.");
        }

        return httpApplication.Request.ToHttpRequestBase();
    }

    public static HttpRequestBase ToHttpRequestBase(this HttpRequest httpRequest)
    {
        if (httpRequest == null)
        {
            throw new ArgumentException("Request is null.");
        }

        return new HttpRequestWrapper(httpRequest);
    }
}

I came across several SO answers that helped me build these extensions:

Solution 4 - asp.net Mvc

I find the following extension methods useful:

    public static HttpContextBase AsBase(this HttpContext context)
    {
        return new HttpContextWrapper(context);
    }

    public static HttpRequestBase AsBase(this HttpRequest context)
    {
        return new HttpRequestWrapper(context);
    }

Usage:

HttpContext.Current.AsBase()
HttpContext.Current.Request.AsBase()

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDaniele ArmanascoView Question on Stackoverflow
Solution 1 - asp.net MvcJamie DixonView Answer on Stackoverflow
Solution 2 - asp.net MvcS.SerpooshanView Answer on Stackoverflow
Solution 3 - asp.net Mvcjwatts1980View Answer on Stackoverflow
Solution 4 - asp.net Mvcrothschild86View Answer on Stackoverflow