asp.net-mvc 如何将 HttpRequest 转换为 HttpRequestBase 对象?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15275370/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 02:37:49  来源:igfitidea点击:

How do I convert an HttpRequest into an HttpRequestBase object?

asp.net-mvchttprequest

提问by Daniele Armanasco

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

我的问题与此相反: 如何将 HttpRequestBase 转换为 HttpRequest 对象?

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

在我的 ASP.NET MVC 应用程序中,我有许多控制器使用的方法,这些控制器接收 HttpRequestBase 作为参数。

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 ...).

现在我必须从另一个方法调用该方法,这不是一个动作(它是一个 nhibernate 拦截器)。在这第二种方法中,我可以访问 HttpContext.Current.Request,这是一个 HttpRequest,我不能将它转换为 HttpRequestBase(由于命名,我认为这是可能的......)。

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

有人知道这些课程有什么关系,我该如何解决我的问题?谢谢你。

回答by Jamie Dixon

You'll want to wrap your HttpRequestin a HttpRequestWrapper:

你会想把你的包裹HttpRequest在一个HttpRequestWrapper

var wrapper = new HttpRequestWrapper(httpRequest);

The HttpRequestWrapperinherits from HttpRequestBase.

HttpRequestWrapper继承HttpRequestBase

回答by S.Serpooshan

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

这是另一种不需要创建新实例的解决方案:

var httpRequestBase = myHttpRequest.RequestContext.HttpContext.Request;

回答by jwatts1980

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.

在我的应用程序中,我有来自几个需要访问 HttpRequestBase 的不同地方的调用。我创建了这组扩展方法来获取并从几种不同的 Http 类型转换为 HttpRequestBase,然后在我需要访问请求时使用 HttpRequestBase 作为接口和类方法的基本类型通过应用程序。

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:

我遇到了几个帮助我构建这些扩展的 SO 答案:

回答by rothschild86

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()