---
title: "Handling an 'A potentially dangerous Request.Form value was detected' exception without deactivating validation"  
description: "Handling an 'A potentially dangerous Request.Form value was detected' exception without deactivating validation"  
author: "Anonymous User"  
published: 2014-12-19  
updated: 2014-12-20  
canonical: https://www.mindstick.com/forum/12799/handling-an-a-potentially-dangerous-request-form-value-was-detected-exception-without-deactivating-validation  
category: "asp.net"  
tags: ["c#", "exception handling", "validation"]  
reading_time: 2 minutes  

---

# Handling an 'A potentially dangerous Request.Form value was detected' exception without deactivating validation

I am creating an ASP.NET [application using C#](https://www.mindstick.com/forum/160510/how-to-fetch-data-from-the-database-in-the-dot-net-console-application-using-c-sharp) for the [scripting](https://www.mindstick.com/blog/183/cross-site-scripting) language. When I enter HTML code into the textboxes on my webpage I get the following [exception](https://www.mindstick.com/articles/1824/objective-c-exception-handling) 'A potentially dangerous Request.Form value was detected', as expected. I would like to be able to catch this exception so that I can put an [error message](https://www.mindstick.com/forum/23174/error-message-the-page-you-are-requesting-cannot-be-served-because-of-the-extension-configuration) out to the user, but I can only find [articles](https://www.mindstick.com/articles/12872/how-to-write-articles-of-50-and-more-a-day-quick-and-easy) on how to disable the [validation](https://www.mindstick.com/articles/12234/validation-using-data-annotation-using-entity-framework); this is not something I'd like to do. Does anybody know where in the ASP.NET [page lifecycle](https://www.mindstick.com/interview/1484/what-is-the-page-lifecycle-of-an-asp-dot-net-mvc) this exception would have to be handled, as I am [having trouble](https://answers.mindstick.com/qa/50524/how-to-fix-iphone-8-that-s-overheating-and-having-trouble-turning-on) catching it.

Thanks you.

## Replies

### Reply by Norman Reedus

I don't know 100% if this would work, but I do something similar for other situations, so I think it will. But try adding an Application_error handler in the global.asax, and look for that exception type, if Server.GetLastError() returns that exception type, try redirecting to your error page. I don't know what the exception type is, but that is easy to find (or just check the message).

Something like:

```
void Application_Error(..){   var ex =Server.GetLastError();   if (ex != null && ex is <whateverexceptiontype>) { // or check ex.Message matches    
HttpContext.Current.Response.Redirect("niceerrorpage.aspx")   }}
```

### Reply by Anonymous User

to allow the html character you need to

change the attribute value of page directive <%@ Page ValidateRequest="false" you can apply this as global level via web.config file inside <system.web> section

<pages validateRequest="false" />


---

Original Source: https://www.mindstick.com/forum/12799/handling-an-a-potentially-dangerous-request-form-value-was-detected-exception-without-deactivating-validation

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
