C# 我无法关闭 ASP.NET MVC 控制器的请求验证

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1110452/
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-08-06 08:28:48  来源:igfitidea点击:

I can't turn off Request Validation for an ASP.NET MVC Controller

c#.netasp.netasp.net-mvcrequest-validation

提问by Ronnie Overby

I am trying to turn off Request Validation for all action methods in a controller by doing this:

我正在尝试通过执行以下操作来关闭控制器中所有操作方法的请求验证:

[ValidateInput(false)]
public class MyController : Controller
{
    ...

The reference I am using says this is possible and tells me to do it this way, but for some reason it's not working.

我正在使用的参考资料说这是可能的,并告诉我这样做,但由于某种原因它不起作用。

If I submit any html (even a simple <b> tag) through a text box, I get the error:

如果我通过文本框提交任何 html(甚至是一个简单的 <b> 标签),我会收到错误消息:

A potentially dangerous Request.Form value was detected from the client (text=<b>").

从客户端检测到潜在危险的 Request.Form 值 (text=<b>")。

It's also not working by attaching the attribute to an individual method.

将属性附加到单个方法也不起作用。

How can I disable Request Validation for a controller?

如何禁用控制器的请求验证?

EDIT

编辑

I am working in VS2008 built in test server.

我在 VS2008 内置测试服务器中工作。

采纳答案by Robert Harvey

I tested it on my machine, on both the class definition and the action method, and it worked for me in both cases. Are you sure your view lines up with your method/controller? Are you putting the attribute on the GET method or the POST method?

我在我的机器上测试了它的类定义和操作方法,它在两种情况下都对我有用。您确定您的视图与您的方法/控制器一致吗?您是将属性放在 GET 方法还是 POST 方法上?

[AcceptVerbs(HttpVerbs.Post)]
[ValidateInput(false)]   
public ActionResult MyAction (int id, string content) {   
    // ...   
}

回答by keithm

Pro ASP.NET MVC Framework (p466) says the following is supposed to work:

Pro ASP.NET MVC Framework (p466) 说以下应该可以工作:

public class MyController : Controller 
{
     public MyController() {
        ValidateRequest = false;
     }
}

回答by Tony Borf

Can you post your controller file and your view file.

你能发布你的控制器文件和视图文件吗?

This works;

这有效;

MytestController--------------------------------

MytestController--------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;

namespace testapp.Controllers
{
    [ValidateInput(false)]
    public class MyTestController : Controller
    {

        public ActionResult Index()
        {
            return View();
        }

    }
}

MyTest(Index)-------------------------------------------------------

MyTest(Index)---------------------------------------------- ---------

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Index</title>
</head>
<body>
 <% using (Html.BeginForm()) { %>
 <%= Html.TextBox("test")%>
 <button type="submit"  >Submit</button>
 <%} %>
</body>
</html>

回答by the_joric

To make it working you need to modify web.config as well:

要使其正常工作,您还需要修改 web.config:

<system.web>
    <httpRuntime requestValidationMode="2.0"/>
    ...
</system.web>