How to redirect Homepage.aspx to an MVC default route?

我只是一个虾纸丫 提交于 2019-11-30 21:40:16

问题


We're developing a new system to replace an existing system.

The new system is ASP.NET MVC so we're defining routes to our controllers and actions as normal.

The old system as traditional ASP.NET so the URLs consist of lots of .aspx pages.

We want to set up redirects so that when a user tries to access /Homepage.aspx (lots of users would have that bookmarked), they'll get redirected to the new system's default route, which is just /

What is the best way for me to do this?

edit: @Chance suggested below that I specify the route:

routes.MapRoute("Homepage", "Homepage.aspx", new { controller = "controller", action = "homepage" });

This works, but I'd like to do a redirect because I don't want Homepage.aspx in the address bar. Is there anything else I can do?


回答1:


You may take a look at the following blog post which illustrates how you could implement a legacy route handler which will perform 301 permanent redirects.




回答2:


Put this code in your global.asax file:

protected void Application_BeginRequest(object sender, EventArgs e)
{    
    if (Request.Url.ToString().ToLower().Contains("homepage.aspx"))
    {            
        // use RedirectPermanent for 301, or Redirect for 302
        Context.Response.RedirectPermanent("/");
    }
}

You could modify this code to apply to other old ".aspx" pages as well.




回答3:


you can just give your default controller/action an additional route that is hardcoded. Ie

routes.MapRoute("Homepage", "Homepage.aspx", new { controller = "controller", action = "homepage" });




回答4:


Another option would be to simply create a static page to replace HomePage.aspx with a meta redirect. This would allow you to present the user with a message informing them that the site has changed, and urge them to update their bookmarks.

You can extend this to the entire site by creating a custom error handler for 404 that points to either a static page, or a specific URL.



来源:https://stackoverflow.com/questions/4626166/how-to-redirect-homepage-aspx-to-an-mvc-default-route

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