C# ASP.NET MVC:输出验证

C# ASP.NET MVC:输出验证,c#,asp.net,xml,asp.net-mvc,C#,Asp.net,Xml,Asp.net Mvc,我在ASP.NET MVC项目中使用以下方法从另一个web服务获取一些XML数据: [HttpPost] [ValidateInput(false)] public ActionResult MyAction() { try { byte[] reqContent = Helper.GetBytes(Request.Unvalidated.Form["xml"]); WebRequest request = WebRequest.Create("

我在ASP.NET MVC项目中使用以下方法从另一个web服务获取一些XML数据:

[HttpPost]
[ValidateInput(false)]
public ActionResult MyAction()
{
    try
    {
        byte[] reqContent = Helper.GetBytes(Request.Unvalidated.Form["xml"]);

        WebRequest request = WebRequest.Create("url");
        request.Method = "POST";
        request.ContentType = "text/xml";
        request.ContentLength = reqContent.Length;
        request.GetRequestStream().Write(reqContent, 0, reqContent.Length);

        string responseXml = null;

        using (WebResponse response = request.GetResponse())
        {
            using (StreamReader reader = new StreamReader(response.GetResponseStream()))
            {
                responseXml = reader.ReadToEnd();
            }
        }

        return Content(responseXml, "text/xml");
    }

    catch(Exception)
    {
        return Json(new { Error = true });
    }
}
动作中的请求工作得很好,我在调试代码时得到了正确的响应。但不幸的是,当我查看Chrome调试工具时,我操作的响应代码(不是使用
WebRequest
发送的请求)为500,错误为:“一个潜在危险的请求。从客户端检测到表单值(xml=somexml)。

是否存在某种输出验证,或者我是否遗漏了其他内容?另外,对
MyAction
控制器方法的POST请求主体由XML数据组成,但是使用
ValidateInput(false)
-属性和
Request
对象的
Unvalidated
-属性,我没有得到任何异常,并且在方法内部工作正常

编辑:解决方案


多亏了我标记为已接受的答案,我不仅更改了最新标准的输入验证,还深入挖掘了可能的原因,并意识到问题在于全局
OutputCacheAttribute
。终于解决了问题。

MVC仍在验证POST请求,然后再执行操作。新的方法是使用
[allowtml]
属性来保存XML<代码>[ValidateInput(false)]已弃用。 看


注:要使
[ValidateInput(false)]
工作,您还需要在web.config中设置
(不推荐)。请参阅。

MVC仍在验证POST请求,然后再执行操作。新的方法是使用
[allowtml]
属性来保存XML<代码>[ValidateInput(false)]已弃用。 看

注:要使
[ValidateInput(false)]
工作,您还需要在web.config中设置
(不推荐)。看

public class PostXmlModel {
    [AllowHtml]
    public string Xml {get; set;}
}

[HttpPost]
public ActionResult MyAction(PostXmlModel postData) {
    string xml = postData.Xml;
    // ...
}