HttpContext,用于.Net 4.5服务器的Context.Request.Unvalidated的替代方法

时间:2014-05-16 17:50:53

标签: c# .net

我的项目中有一个方法,如下所示,它采用HttpContext个对象并返回string

//returns the xml document as string
private static string GetXmlReceiptFromContext(HttpContext context)
{
    context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
    context.Response.Cache.SetNoStore();
    context.Response.Cache.SetExpires(DateTime.MinValue);        
    return context.Request.Unvalidated.Form.Get("status");
}

这样做的结果最终会传递到一个非常重要的方法,需要这个字符串。

Context.Request.Unvalidated似乎只能从.Net 4.5获得。 我需要为没有.Net 4.5的服务器替代这种方法,并且将使用这个程序集。

任何人都可以建议另一种访问和返回状态参数的方法,该参数的值将是来自上下文的XML文档,而不使用Context.Request.Unvalidated?

修改

这不适用于webform或MVC项目,我们开发了一个类库,我们理想地想要包含程序集中的所有支付相关功能,即单一责任,我们的前端应用程序将使用它不需要了解事情的付款方面。

2 个答案:

答案 0 :(得分:5)

我不知道您使用的是MVC还是Web窗体/网页,但有几种解决方案可供您使用。有关禁用请求验证的详细信息,请查看this MSDN page

Web窗体:将<@ Page validateRequest="false" %>添加到页面顶部以禁用单个页面的验证(MSDN页面中有更多页面/部分应用程序的其他选项)。

MVC:将属性[ValidateInput(false)]添加到您的操作顶部或向您要绑定的模型中的属性添加[AllowHtml]属性。

答案 1 :(得分:2)

您可以使用BinaryRead并自行解析原始请求正文。请参阅下面的RawPostValues()方法。

public class Handler1 : IHttpHandler
{
    public bool IsReusable
    {
        get { return false; }
    }

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        context.Response.Write(GetXmlReceiptFromContext(context));
        context.Response.Write("</ br>");
        context.Response.Write(GetXmlReceiptFromContext(context));
    }

    //returns the xml document as string
    private static string GetXmlReceiptFromContext(HttpContext context)
    {
        context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
        context.Response.Cache.SetNoStore();
        context.Response.Cache.SetExpires(DateTime.MinValue);
        return RawPostValues(context)
            .SingleOrDefault(kvp => kvp.Key.Equals("status", StringComparison.InvariantCultureIgnoreCase))
            .Value;
    }

    private static IEnumerable<KeyValuePair<string, string>> RawPostValues(HttpContext context)
    {
        if (context.Request.HttpMethod != "POST") yield break;

        var tmpPosition = context.Request.InputStream.Position;
        string[] formElements;

        try
        {
            formElements = System.Text.Encoding.Default.GetString(
                context.Request.BinaryRead(context.Request.ContentLength))
                .Split('&');

            if (formElements.Length < 1) yield break;
        }
        finally
        {
            context.Request.InputStream.Position = tmpPosition;
        }

        foreach (var element in formElements)
        {
            if (string.IsNullOrEmpty(element)) continue;

            var key = element.Substring(0, element.IndexOf('='));
            var value = element.Substring(key.Length + 1, element.Length - key.Length - 1);
            yield return new KeyValuePair<string, string>(key, value);
        }
    }
}

Fiddler2的一些快速烟雾测试显示这是有效的。你可能不得不敲出一些扭结。

请求

POST http://localhost:48707/Handler1.ashx HTTP/1.1
Host: localhost:48707
Content-Length: 19
content-type: application/x-www-form-urlencoded

status=<b>oops!</b>

<强>响应

HTTP/1.1 200 OK
Server: ASP.NET Development Server/11.0.0.0
Date: Thu, 22 May 2014 05:29:26 GMT
X-AspNet-Version: 2.0.50727
Cache-Control: no-cache, no-store
Pragma: no-cache
Expires: -1
Content-Type: text/plain; charset=utf-8
Content-Length: 12
Connection: Close

<b>oops!</b>