Custom 404 controller

Topics: Customizing Orchard, Writing modules
May 1, 2012 at 11:33 PM
Edited May 1, 2012 at 11:33 PM

I need to redirect a certain, very complex class of URL's to a different website altogether. To take one example, if the URL http://alanta.com/ken 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 http://app.alanta.com/ken. 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:

[Themed]
[OrchardSuppressDependency("Orchard.Core.Common.Controllers.ErrorController")]
public class ErrorController : Controller
{

	public ActionResult NotFound(string url)
	{
		// If this is a valid room URL, redirect to app.alanta.com.
		if (RepositoryHelper.IsRoomUrl(null, url))
		{
			return Redirect("http://app.alanta.com/" + 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., http://www.davidhayden.me/this-page-does-not-exist), 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?

Coordinator
May 1, 2012 at 11:51 PM

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

May 2, 2012 at 12: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.