This project is read-only.

Custom 404 controller

Topics: Customizing Orchard, Writing modules
May 2, 2012 at 12:33 AM
Edited May 2, 2012 at 12:33 AM

I need to redirect a certain, very complex class of URL's to a different website altogether. To take one example, if the URL comes in and would normally trigger a 404 error, I'd like to check an account database to see if the parameter "ken" matches an existing user, and then redirect that request to If the URL "ken" doesn't match an existing user, I need to display a normal 404 page.

I've had luck in the past with overriding some of Orchard's built-in controllers, so I thought that something like this (in my business-specific custom module) would work for me:

public class ErrorController : Controller

	public ActionResult NotFound(string url)
		// If this is a valid room URL, redirect to
		if (RepositoryHelper.IsRoomUrl(null, url))
			return Redirect("" + url);
		return HttpNotFound();

Unfortunately, when I do that, that code never gets hit, and I just get a standard IIS 404 page (as opposed to the custom Orchard 404 page).

David Hayden describes a couple different ways to do this (e.g.,, but with the custom logic I need, it doesn't seem like placing an extra NotFound.cshtml view in my theme is the right approach.

Any other thoughts about the best way to do this?

May 2, 2012 at 12:51 AM

You can clone the ErrorController and give the route that triggers it a higher priority.

May 2, 2012 at 1:41 AM

That's precisely what I was trying to do. I've cloned the ErrorController, I've created a route pointing to it (with a priority of int.MaxValue), I've suppressed the original controller (and also tried not suppressing it) . . . not sure what else to try.