Reputation: 4713
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:
Upvotes: 5
Views: 6662
Reputation: 864
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.
Upvotes: 3
Reputation: 4713
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.
Upvotes: 7
Reputation: 6655
I would just return a default view, perhaps a 404 response would be appropriate. Of course to make this work, you'll need to change your architecture a bit.
AFAIK you will need to have some code to check to see if the file exists, as in your ASPX file if you don't want the default framework behavior. You might ought to use ResolveURL or Url.Content to resolve the path from you application path.
As a note, generally, it is not exactly safe to make your domain (the file names) exposed to the world. If the ASPX's are just data, then you should put the text into a database or plain, non-executable text files. If you are building some sort of CMS (i.e. you really want a way of 'installing' functionality), then you might consider using an ASPX view to contain partial views to encapsulate the chunks of ASP.NET code.
As for getting to the 404 page, this answer might help.
Upvotes: 0
Reputation: 27411
I think the best place to check whether the view exist would be within the controller. If you encapsulate the constructor of the view is not working, then it might be the ASP.net defer the instantiation of View outside the constructor (not sure if it is).
But one thing you might try is to manually check and see if the path exist or not, using Server.MapPath() to get the absolute file path and check if it is existing.
Upvotes: 0