Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7507019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T22:24:09+00:00 2026-05-29T22:24:09+00:00

My question is hopefully pretty straight forward. Upon submitting my form, I would like

  • 0

My question is hopefully pretty straight forward.
Upon submitting my form, I would like to flag all properties (fields) that have invalid characters (specifically HTML) as model errors.
The issue I am facing is that Request Validation kicks in before model binding/validation and I get HTTP 500…
I saw a similar question that advertises either using [AllowHtml] property attribute on your Model/ViewModel or using <httpRuntime requestValidationMode="2.0" /> in web.config, but what I am looking for is how to “globally catch Request Validation exceptions and show them as model errors“. Furthermore, I don’t want to “strip” HTML tags, I want to notify the user that their input is not valid.

I thought about using Regular Expression validation attributes to find bad input, but as I mentioned, the Request Validation on ASP.NET MVC3 occurs before model binding/validation, so that is a no-go…

A really good overview of the Request Validation can be found here.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T22:24:10+00:00Added an answer on May 29, 2026 at 10:24 pm

    I think your only intention is to get the Request Validation exception during model binding and show the errors as model state error. Here is the sample,

        using System.Web.Helpers;
        public class MyModelBinder : DefaultModelBinder
        {
            public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
            {
                try
                {
                    return base.BindModel(controllerContext, bindingContext);
                }
                catch(HttpRequestValidationException ex)
                {
                    var modelState = new ModelState();
                    modelState.Errors.Add(ex.Message);
                    var key=bindingContext.ModelName;
                    var value = controllerContext.RequestContext.HttpContext.Request.Unvalidated().Form[key];
                    modelState.Value = new ValueProviderResult(value, value,CultureInfo.InvariantCulture);
                    bindingContext.ModelState.Add(key, modelState);
                }
                return null;
            }
        }
    
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            ModelBinders.Binders.DefaultBinder = new MyModelBinder();
        }
    

    This will add the exception of only request validation in model state. Sorry, If I don’t understand your question clearly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just a quick regex question...hopefully I have a string that looks something like this:
hopefully a pretty simple question this time. I have a Select method in a
This is probably (hopefully) a pretty simple question, but I can't seem to get
Hopefully this is a pretty simple question for someone to answer. I have a
Hopefully this is a pretty simple question! I've got the code working in jQuery
This might be a pretty basic question, or peculiar to my set up (hopefully
I have a question that has pretty much been asked here: asp.net mvc Html.ActionLink()
EDIT: Upon re-reading my original question I realized very quickly that it was very
I have a hopefully pretty simple question here. I'm converting some Access SQL script
This is pretty much a duplicate of this unanswered question , but hopefully someone

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.