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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T05:28:34+00:00 2026-05-12T05:28:34+00:00

I have an asp.net mvc 1.0 site that serves some content from a 2

  • 0

I have an asp.net mvc 1.0 site that serves some content from a 2 level hierarchy
/category/article

When things work right the article maps to a view and the view gets rendered. However, when the url meets the routing condition but the view doesn’t exist an exception is raised that I can’t trap in the Controller action.

Routing:

routes.MapRoute(
  "Article",
  "{category}/{article}.aspx",
  new { controller = "MyController", action = "Article" }
);

MyController Action:

public ActionResult Article(string category, string article)
{
    string path = string.Format("~/Views/{0}/{1}.aspx", category, article);
    ViewResult vr = View(path);
    return vr;
}

However, when the view is not found, a System.InvalidOperationException is generated that I can’t catch in the Controller Action.

Exception Details: System.InvalidOperationException: The view
‘~/Views/my-category/my-article-with-long-name.aspx’ or its master
could not be found. The following locations were searched:
~/Views/my-category/my-article-with-long-name.aspx

I can trap the error in the Application_Error() method in global.asax.cs but:

  1. don’t know how to redirect to the error view from there
  2. wonder if there is a better place closer to where the exception is
    raised.
  • 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-12T05:28:34+00:00Added an answer on May 12, 2026 at 5:28 am

    xandy, Greg,
    I appreciate your answers. This article (Strategies For Resource Based 404 Errors in aspnet mvc) helped me derive the solution I was looking for in a pretty clean way. All I need to do is override Controller.OnException. Since I only have one controller where I need the behavior I only have to override OnException in that controller.

    That being said, my solution treats the symptoms not the disease and as both of you suggest, it would be better to check for the file’s existence before invoking this.View on a path.

    Here is the code I used to treat the symptoms 🙂

    protected override void OnException(ExceptionContext filterContext)
    {
        //InvalidOperationException is thrown if the path to the view
        // cannot be resolved by the viewengine
        if (filterContext.Exception is InvalidOperationException)
        {
            filterContext.ExceptionHandled = true;
            filterContext.Result = View("~/Views/Error/NotFound.aspx");
            filterContext.HttpContext.Response.StatusCode = 404;
        }
    
        base.OnException(filterContext);
    }
    

    One issue I couldn’t resolve is how to display the NotFound View in a clean way. It is usually accessed via the ErrorController NotFound Action. I had to hardcode the path to it. I can live with this but would like to know if it is possible w/o the HC path.

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

Sidebar

Related Questions

I have some questions about some things in Asp.net MVC that still confuses me.
I have a asp.net mvc site that references a couple of libraries. Recently I
I have an ASP.NET MVC 3 site that makes use of a ClientDependency framework
I have an ASP.NET MVC site and ELMAH is showing me that my site
I have a ASP.NET MVC site using Membership Provider. I have trouble testing some
I have a asp.net mvc site that being hosted via IIS 7. I believe
Ok, so this is my dilemma... I have an ASP.NET MVC site that is
I have an ASP.NET (MVC) website that is serving static content (images) as well
I have a lot of content-heavy views in my ASP.NET MVC 2 site. These
I have an ASP.NET MVC intranet site that uses Windows Authentication (Kerberos) exclusively with

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.