I have a custom route set up:
var tradeCategoriesRoute = routes.MapRoute(
name: "TradeCategoriesIndex",
url: "TradeCategories/{*categories}",
defaults:
new
{
controller = "TradeCategories",
action = "Index"
},
namespaces: new[] {"Website.Controllers"}
);
tradeCategoriesRoute.DataTokens["UseNamespaceFallback"] = false;
tradeCategoriesRoute.RouteHandler = new CategoriesRouteHandler();
I also have a custom 404 page set up in my Global.asax:
private void Application_Error(object sender, EventArgs e)
{
var exception = Server.GetLastError();
var httpException = exception as HttpException;
DisplayErrorPage(httpException);
}
private void DisplayErrorPage(HttpException httpException)
{
Response.Clear();
var routeData = new RouteData();
if (httpException != null && httpException.GetHttpCode() == 404)
{
routeData.Values.Add("controller", "Error");
routeData.Values.Add("action", "Missing");
}
else if (httpException != null && httpException.GetHttpCode() == 500)
{
routeData.Values.Add("controller", "Error");
routeData.Values.Add("action", "Index");
routeData.Values.Add("status", httpException.GetHttpCode());
}
else
{
routeData.Values.Add("controller", "Error");
routeData.Values.Add("action", "Index");
routeData.Values.Add("status", 500);
}
routeData.Values.Add("error", httpException);
Server.ClearError();
Response.TrySkipIisCustomErrors = true;
IController errorController = ObjectFactory.GetInstance<ErrorController>();
errorController.Execute(new RequestContext(new HttpContextWrapper(Context), routeData));
Response.End();
}
It seems that my real problem is the custom route handler that I made:
public IHttpHandler GetHttpHandler(RequestContext requestContext)
{
IRouteHandler handler = new MvcRouteHandler();
var values = requestContext.RouteData.Values;
if (values["categories"] != null)
values["categoryNames"] = values["categories"].ToString().Split('/').Where(x => !string.IsNullOrWhiteSpace(x)).ToArray();
else
values["categoryNames"] = new string[0];
return handler.GetHttpHandler(requestContext);
}
It works fine and properly displays the 404 page for routes like "/doesnotexist" but doesn't work for routes like "/TradeCategories/doesnotexist". Instead I get a built in 404 page with the message "The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.".
How can I get my custom 404 page working with these custom routes?