views:

87

answers:

2

This has already been asked here but I was hoping there was a nicer "routing" way to do this.

Essentially I want to redirect to the Home/Index page when a user enters an incorrect url in my site.

EDIT

I'm using IIS.

+2  A: 

IMHO the best way will be to use Home/Index as 404 error handling page. So user will be redirected to a home page each time 404 is returned.

<?xml version="1.0"?>
<configuration>
    <system.web>
        <!-- For IIS6 and Cassini --!>
        <customErrors mode="RemoteOnly">
            <error redirect="Home/Index" statusCode="404"/>
        </customErrors>
    </system.web>

    <system.webServer>
        <!-- For IIS7 --!>
        <httpErrors>
            <error statusCode="404" path="Home/Index" /> 
        </httpErrors> 
    </system.webServer>
</configuration>

Or use IIS7 Rewrite module.

Artem Tikhomirov
I like this but she no work for me. I know it should but it don't. If I enter /site/Slappy I get the ugly "Resource cannot be found" error page.
griegs
Are you on IIS7 or Cassini?
Artem Tikhomirov
Cassini at the moment. Yeah that would make a difference huh? :) +1 Nice pick up.
griegs
Remove mode="RemoteOnly" attribute than. It helps you to debug, so you see original errors, while remote people receive Home/Index.
Artem Tikhomirov
BTW, I hate Microsoft for double configuration for web servers. At least they could make Cassini understand <system.webServer> config. URRHHH!
Artem Tikhomirov
I'm going to accept this answer. Still doesn't work for me but that's probably due to my environment which I need to get right first. Thanks @Artem
griegs
Try customErrors mode="On".
Artem Tikhomirov
hehe, already tried that.
griegs
Not at all, griegs.
Artem Tikhomirov
+1  A: 

Or you could implement your own Route concrete class accepting every input and repopulating routing dictionary with values: action="Index", controller="Home" and removing everything else from it.

You should add that's implementations instance as last to the routing collection.

Artem Tikhomirov
Kinda the same as the post I linked to in my question which is increasingly looking like the solution to implement.
griegs
I don's recommend that. You therefore break HTTP protocol. Google and other spiders will continually give the same page and may ban you for it. IMHO again, 404 should be returned. May be with your home page content, but it should be returned.
Artem Tikhomirov
+1, yeah not a bad point
griegs