开发者

Redirect to Home/Index when page not found

开发者 https://www.devze.com 2022-12-15 07:22 出处:网络
This has already been asked here but I was hoping there was a nicer \"routing\" wa开发者_StackOverflow社区y to do this.

This has already been asked here but I was hoping there was a nicer "routing" wa开发者_StackOverflow社区y 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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消