簡體   English   中英

基本Html.ValidationMessageFor不起作用

[英]Basic Html.ValidationMessageFor not working

我在我們的網站上有一個簡單的反饋表。 當用戶在頁面中輸入html時,他們應該收到驗證錯誤消息。 但是他們沒有。 引發並捕獲了異常:

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

這里捕獲的異常是:

從客戶端檢測到潛在的危險Request.Form值

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

這是Asp.net MVC標記:

<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="you@youremailaddress.com" }) %>
          <%= 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="you@youremailaddress.com" 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。 因此,客戶端和服務器端驗證均失敗。

怎么了?

從技術上講,這不是Model的錯誤,這就是為什么您無法使用Model.IsValid代碼語句來處理它。

如果你真的允許HTML在這個領域提交或要消毒提交的值在服務器上,你可以注釋Comment與模型的性質[AllowHtml]屬性。 另外,如果您想通知用戶它們不能在文本中包含標簽,則可以在服務器端使用ModelState.AddModelError()方法進行反饋解析期間向ModelState添加錯誤。

嘗試將[ValidateInput(false)]屬性添加到控制器操作方法。

來源: http//msdn.microsoft.com/en-us/library/system.web.mvc.validateinputattribute.aspx

編輯:如果仍然要驗證,則應在將輸入發送到服務器之前對其進行編碼,例如使用HttpUtility.HtmlEncode 驗證阻止代碼的原因是它可能是惡意的。 通過編碼,您可以允許它通過並仍然啟用驗證。 但是,您可能需要更改驗證規則/屬性以說明編碼。 如果以前不清楚,則此選項要求您一旦到達控制器就在服務器端進行驗證,根據需要向ModelState添加錯誤,並在出現錯誤時根據需要返回相同的視圖或部分視圖。

顯然,將請求驗證保留在原處是有好處的,並且正確地禁用請求驗證是不應該輕視的。 但是在ASP.NET中沒有合適的方法來處理此異常,因為它在管道中這么早就發生了。 這是保護您免受自身傷害的實際框架。

您可以使用原始帖子中指出的Application_Error事件。 拿出一些代碼,例如:

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
    }
}

在MVC 3及更高版本中,您可以將AllowHtml屬性與可以包含一些html的屬性一起使用

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM