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

  • Home
  • SEARCH
  • 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 8140061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T12:02:23+00:00 2026-06-06T12:02:23+00:00

I have a custom model binder which pulls an implementation of an interface from

  • 0

I have a custom model binder which pulls an implementation of an interface from a MEF container. It is implemented as follows:

public class PetViewModelBinder : DefaultModelBinder
{
    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        var petId = bindingContext.ValueProvider.GetValue("id");
        var container = controllerContext.HttpContext.Application[MvcApplication.PLUGINS] as CompositionContainer;
        Lazy<IPet, IPetMetadata> pet = null;
        try
        {
            pet = container.GetExport(typeof(IPet), petId);
            var petVM = new Models.PetViewModel(pet);
            bindingContext.ModelMetadata.Model = petVM;

            return base.BindModel(controllerContext, bindingContext);
        }
        catch (Exception)
        {

            throw;
        }
        finally
        {
            container.ReleaseExport(pet);
        }

    }

This works splendidly when MEF has an Export of petId… but returns http status 500 (server error) when an Export does not exist. Error message obfuscation requirements dictate http status 403 (forbidden) should be returned.

What can be done to trap the error, change the response status, and either not return content, or re-route the Action to handle this condition?

  • 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-06-06T12:02:25+00:00Added an answer on June 6, 2026 at 12:02 pm

    If you want to return a particular http status code you should do that from a controller or action filter.

    One way to do this is to return null from your model binder and handle that in your controller. This is a bit coarse however so you won’t be able to distinguish between different errors.

    Another way to do it would be to throw a specific exception and handle that in your (global) error handling. A customized HandleError action filter could do this:

    public class CustomHandleErrorAttribute : HandleErrorAttribute
    {
      public int StatusCode { get; set; }
    
      public override void OnException( ExceptionContext filterContext )
      {
         base.OnException( filterContext );
         if ( StatusCode > 0 )
         {
            filterContext.HttpContext.Response.StatusCode = StatusCode;
         }
      }
    }
    

    In your controller, decorate the action with this attribute:

    [CustomHandleError( ExceptionType = typeof (NotAllowedException), View = "~/Views/Shared/Error.cshtml",
         StatusCode = 403 )]
    public ActionResult Index( FancyModel model )
    {
       return View( model );
    }
    

    Finally, in your model binder throw a NotAllowedException, which is a custom exception type you’ll also need to define.

    Note that this will only work on your development setup if you have enabled custom errors in your web.config file.

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

Sidebar

Related Questions

I'm using ASP.NET MVC 3 and have my custom model binder. public class NewsModelBinder
I have a custom model binder inheriting from DefaultModelBinder. What I want it to
I have a custom model class which contains a decimal member and a view
I have created a custom Model Binder that inherits from DefaultModelBinder, and overriding the
Firstly, bear with me here. I have a custom model binder which is successfully
I have a custom model binder to get data from session, but I also
In my application I have a custom model binder that I set to the
This is my custom model binder. I have my breakpoint set at BindModel but
I have a model class that uses a custom validator as shown in following
I want to create a custom modelbinder which validates the bounded model. I have

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.