RequestValidationSource

RequestValidationSource

本文介绍了基本Html.ValidationMessageFor不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我们的网站上有一个简单的反馈表.当用户在页面中输入html时,他们应该收到验证错误消息.但是他们没有.引发并捕获了异常:

I have a simple feedback form on our website. When users enter html in the page, they should get a validation error message. But they don't. The exception is thrown and caught in:

void Application_Error(object sender, EventArgs e)
{
   ...
}

这里捕获到的异常是:

在System.Web.HttpRequest.ValidateString(字符串值,字符串collectionKey,RequestValidationSource requestCollection)

at System.Web.HttpRequest.ValidateString(String value, String collectionKey, RequestValidationSource requestCollection)

这是Asp.net MVC标记:

Here is the Asp.net MVC markup:

<form action="<%=Url.Action("Create") %>" method="post" class="data-entry-form">
<fieldset class="comment">
    Your thoughts:
    <div class="editor-field">
        <%= Html.TextAreaFor(model => model.Comment, 10, 2, new { placeholder="your message" }) %>
        <%= Html.ValidationMessageFor(model => model.Comment) %>
    </div>
    <br />

    E-mail address (optional)
      <div class="editor-field">
          <%= Html.TextBoxFor(model => model.Email, new { placeholder="[email protected]" }) %>
          <%= Html.ValidationMessageFor(model => model.Email) %>
      </div>
          <input type="submit" value="Send" />
</fieldset>
</form>

这是生成的html:

    <form action="/feedback/Create/" method="post" class="data-entry-form">
    <fieldset class="comment">
        Your thoughts:
        <div class="editor-field">
            <textarea cols="2" id="Comment" name="Comment" placeholder="your message" rows="10">
            </textarea>
        </div>
        <br />

        E-mail address (optional)
        <div class="editor-field">
           <input id="Email" name="Email" placeholder="[email protected]" type="text" value="" />
        </div>
        <input type="submit" value="Send" />
    </fieldset>
    </form>

这是控制器代码:

[HttpPost]
public ActionResult Create(Feedback feedback)
{
    if (ModelState.IsValid)
    {
        FillFeedbackObject(feedback);
        feedbackRepository.AddFeedback(feedback);
        return View("SuccessMessage", new SuccessMessageModel("Thanks for your message!"));
    }
    return View(feedback);
}

我在web.config文件的appSettings部分中还具有< add key ="ClientValidationEnabled" value ="true"/> ,并且包括了jquery.validate.js.因此,客户端和服务器端验证都将失败.

I also have <add key="ClientValidationEnabled" value="true" /> in the appSettings section of the web.config file and I am including jquery.validate.js. So both client side and server side validation fail.

怎么了?

推荐答案

很明显,将请求验证保留在原处是有好处的,并且您正确地禁用请求验证不应该被轻视.但是在ASP.NET中没有合适的方法来处理此异常,因为它发生在管道的早期.这是保护您免受自己伤害的实际框架.

Obviously, leaving request validation in place has it's advantages, and you're correct disabling request validation shouldn't be taken lightly. But there's no graceful way to handle this exception in ASP.NET because it happens so early in the pipeline. It's the actual framework protecting you from yourself.

您可以使用在原始帖子中指出的 Application_Error 事件.拿出一些代码,如:

You can use the Application_Error event that you pointed out in your original post. The come up with some code like:

protected void Application_Error(object sender, EventArgs e)
{
    var exception = Server.GetLastError();
    if (exception is HttpRequestValidationException)
    {
        // use Response.Redirect to get the user back to the page, and use session/querystring to display an error
    }
}

这篇关于基本Html.ValidationMessageFor不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 01:46