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:
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.
There is a suitable place to handle this. If you implement your own ViewEngine, you can override the "FileExists" method.
public class ViewEngine : RazorViewEngine
{
protected override bool FileExists(ControllerContext context, string path)
{
if(!base.FileExists(context, path))
throw new NotFoundException();
return true;
}
}
You just need to register the view engine in your Global.asax like so,
ViewEngines.Engines.Clear();
ViewEngines.Engines.Add(new ViewEngine());
In Application_Error you can implement a handler that catches NotFoundExceptions, logs them, then returns a friendly message via executing an ErrorController.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With