2011-06-10 1 views
0

이 오류를 해결하기 위해 컨트롤러/동작에 대한 HttpRequestValidation을 해제하는 방법에 대한 여러 게시물을 보았습니다.하지만 대부분의 경우이 기능을 활성화하려고합니다. 나는 주위에 try/catch 블록을 포장 기꺼이 검증 우회 할 몇 충분한 경우가 있습니다 : 다시ASP.Net MVC HttpRequestValidationException - 캐치 오류 및 캡처 입력?

string searchTerm = string.Empty; 
try { 
    searchTerm = Request.QueryString["q"]; 
} catch (HttpRequestValidationException ex) { 
    // What can I do here to capture the value? Is parsing the error message the only way? 
    // Seems like ex.Data property would be a good place for Microsoft to stick values if 
    // people wanted to do something with them, but ex.Data is empty. 
} 

는, 이것은 단지 형태 내의 특정 필드에, 그래서 싶지 않아 전체 컨트롤러 또는 동작에 대한 유효성 검사를 비활성화하십시오. 특별히 처리하지 않은 분야에 대한 추가 보호 기능을 원합니다.

답변

2

AllowHTML 속성을 확인 했습니까? 그것은 정확히 당신이 필요로하는 것일 수도 있습니다.

public class MyModel 
{ 
    public string ValidatedField {get; set;} // no HTML allowed here! 

    [AllowHtml] 
    public string NonValidatedField {get; set;} // user can enter HTML 
} 
+0

완벽하게하십시오. 뷰 문자열을 쿼리 문자열에서 직접 가져 오는 대신보기 모델을 만들면됩니다. – Sam