Redirect to Home/Index when page not found

谁都会走 提交于 2019-12-13 19:02:14

问题


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.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/1983131/redirect-to-home-index-when-page-not-found

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!