1

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.

Community
  • 1
  • 1
griegs
  • 22,624
  • 33
  • 128
  • 205

2 Answers2

2

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.

Demodave
  • 6,242
  • 6
  • 43
  • 58
Artem Tikhomirov
  • 21,497
  • 10
  • 48
  • 68
  • 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 Dec 31 '09 at 00:42
  • Cassini at the moment. Yeah that would make a difference huh? :) +1 Nice pick up. – griegs Dec 31 '09 at 00:51
  • Remove mode="RemoteOnly" attribute than. It helps you to debug, so you see original errors, while remote people receive Home/Index. – Artem Tikhomirov Dec 31 '09 at 00:56
  • 1
    BTW, I hate Microsoft for double configuration for web servers. At least they could make Cassini understand config. URRHHH! – Artem Tikhomirov Dec 31 '09 at 00:58
  • 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 Dec 31 '09 at 01:02
1

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
  • 21,497
  • 10
  • 48
  • 68
  • Kinda the same as the post I linked to in my question which is increasingly looking like the solution to implement. – griegs Dec 31 '09 at 00:44
  • 1
    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 Dec 31 '09 at 00:53