I'm trying to implement custom error handlers in Umbraco with MVC, based on this topic on SO.
What I've got is the following:
public class Global : Umbraco.Web.UmbracoApplication
{
protected void Application_EndRequest()
{
if (Context.Response.StatusCode == 404)
{
Response.Clear();
var contextBase = new HttpContextWrapper(HttpContext.Current);
using (var context = UmbracoContext.EnsureContext(contextBase, ApplicationContext.Current, new WebSecurity(contextBase, ApplicationContext.Current), UmbracoConfig.For.UmbracoSettings(), UrlProviderResolver.Current.Providers, false))
{
var publishedContent = UmbracoContext.Current.ContentCache.GetByRoute("/404");
// Todo: change the current request routing to the 404 page
}
}
}
}
The next step would be to change the current request/routing to show the /404 page. I'm ensuring the UmbracoContext because I'm requesting a static URL in this case ('/somename.jpg').
That would be a shame. This works, but isn't based on a page in Umbraco, just a controller and a view without CMS data:
public class Global : Umbraco.Web.UmbracoApplication
{
protected void Application_EndRequest()
{
if (Context.Response.StatusCode == 404)
{
Response.Clear();
var rd = new RouteData();
rd.Values["controller"] = "Errors";
rd.Values["action"] = "NotFound";
var contextBase = new HttpContextWrapper(HttpContext.Current);
using (var context = UmbracoContext.EnsureContext(contextBase, ApplicationContext.Current, new WebSecurity(contextBase, ApplicationContext.Current), UmbracoConfig.For.UmbracoSettings(), UrlProviderResolver.Current.Providers, false))
{
IController c = new ErrorsController();
c.Execute(new RequestContext(new HttpContextWrapper(Context), rd));
}
}
}
}
In my previous solution I did a WebRequest to '/404' and wrote the result to the current Response, but I would really like to do it without WebRequest this time.
Custom error handlers
I'm trying to implement custom error handlers in Umbraco with MVC, based on this topic on SO.
What I've got is the following:
The next step would be to change the current request/routing to show the /404 page. I'm ensuring the UmbracoContext because I'm requesting a static URL in this case ('/somename.jpg').
Any hints on how to do this?
Might be wrong, but in Application_EndRequest it is too late for Umbraco to render a page, so your best bet would be a redirect.
(ok, that might be a quick answer... need to look into more details)
That would be a shame. This works, but isn't based on a page in Umbraco, just a controller and a view without CMS data:
In my previous solution I did a WebRequest to '/404' and wrote the result to the current Response, but I would really like to do it without WebRequest this time.
is working on a reply...