I want disable RequestValidation on particular view in ASP.NET MVC 2.0. So I added some necessary to view Page directive section as below:
<%@ Page ValidateRequest="false" Language="C#" MasterPageFile="Path" 开发者_Go百科Inherits="System.Web.Mvc.ViewPage<Path>" %>
but still no go.
Even tried adding this in the Web.Config:
<httpRuntime requestValidationMode="2.0" />
Did you also add [ValidateInput(false)]
attribute on the action?
I used this method, as this is the only way I found it to work.
I replaced the invalid character with a valid one.
Ex) strUrl = strUrl.Replace("&", "-")
Why dont you create a viewmodel for your object that you post back. You can use Regular Expressions to prevend illegal characters, like < and >
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
namespace Web.Models.ViewModels
{
public class Account
{
public Guid Id { get; set; }
[Required(ErrorMessage = "You have to enter this")]
[StringLength(100, ErrorMessage = "Too much text")]
[RegularExpression(@"^([a-zA-Z0-9_\-\.\s]+)", ErrorMessage = "Not allowed")]
public string Firstname { get; set;
}
}
精彩评论