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 895229
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T14:31:34+00:00 2026-05-15T14:31:34+00:00

I have a custom ActionResult for returning certain HTTP Errors, like NotFoundResult and ForbiddenResult,

  • 0

I have a custom ActionResult for returning certain HTTP Errors, like NotFoundResult and ForbiddenResult, they all derive from ViewResult.

I use them for instances like short circuiting actions with a 404 if an entity was not found in the database during the course of an action.

Within these result objects, I set the HTTP Status to the appropriate number. When I do that, the view that these ViewResults reference does not render. I have to leave the status as 200 OK in order for my view to be rendered.

How do I set an appropriate status AND render a view in ASP.NET MVC 2.0?

  • 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-15T14:31:35+00:00Added an answer on May 15, 2026 at 2:31 pm

    I have a custom ActionResult for
    returning certain HTTP Errors, like
    NotFoundResult and ForbiddenResult,
    they all derive from ViewResult.

    Allow me to suggest you an alternative error handling:

    Start by creating an error controller and the corresponding views:

    public class ErrorController : Controller
    {
        public ActionResult General()
        {
            return View();
        }
    
        public ActionResult HttpError404()
        {
            return View();
        }
    
        public ActionResult HttpError500()
        {
            return View();
        }
    }
    

    In Global.asax define the Application_Error method:

    protected void Application_Error(object sender, EventArgs e)
    {
        var exception = Server.GetLastError();
        // TODO: Log the exception with your favorite logging framework
    
        Response.Clear();
        var httpException = exception as HttpException;
    
        var routeData = new RouteData();
        // Take the ErrorController
        routeData.Values.Add("controller", "error");
    
        if (httpException == null)
        {
            // Use the General action for any unhandled error
            routeData.Values.Add("action", "general");
        }
        else
        {
            switch (httpException.GetHttpCode())
            {
                case 404:
                    routeData.Values.Add("action", "httpError404");
                    break;
                case 500:
                    routeData.Values.Add("action", "httpError500");
                    break;
                default:
                    routeData.Values.Add("action", "general");
                    break;
            }
        }
    
        // Add the exception to route data so that the error controller 
        // could use it with RouteData.Values["error"]
        routeData.Values.Add("error", exception);
    
        Server.ClearError();
        IController errorController = new ErrorController();
        errorController.Execute(new RequestContext(new HttpContextWrapper(Context), routeData));
    }
    

    Finally throw appropriate exceptions:

    public class HomeController: Controller
    {
        public ActionResult Index(int id)
        {
            var model = _repository.GetModel(id);
            if (model == null)
            {
                throw new HttpException(404, "Model not found with id = " + id);
            }
            return View(model);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have custom event that has several different subscribers who will all use the
I have custom errors turned on in webconfig and redirecting to /Error/Trouble. This is
We have recently upgraded from mvc2 to mvc3 and one of our Custom Authorization
I have an action that looks like this: [Post] [PopulateModelFromId] public ActionResult ChangeName( string
I have a custom ActionResult which is more deeply encountering System.Web.HttpException when the remote
I have created a custom Model Binder that inherits from DefaultModelBinder, and overriding the
I have a controller method with a custom FilterAttribute on it... [ActivityHistory] public ActionResult
I have custom UITableViewCell . It contains UITextLabel . When I press this cell
I have custom validation rule: public function customRule($check) { } Inside this rule I
I have custom classes that I currently instantiate within App.xaml as resources. I want

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.