ASP.Net MVC - Trapping certain URL's to do 301 Redirect

谁都会走 提交于 2019-12-01 12:39:28

问题


I am moving from an old site design to a new design with new URL's.

All previous page names were static files called PageXX.html, PageX.html, Index.html - where X is a number.

My site is now dynamic but I want to trap for those 3 incoming url's and then try and redirect to a certain new page (301 Redirect) else send them to the home page.

Do I do all of this in Global.asax or do I just trap those Url's in Global.asax and then route it to a Action and do a 301 Redirect in the Action?

Any code examples would help a lot!

Thanks

EDIT: I think what needs to be done is trap the routes in Global.asax and then send them to a Action which will work out where to send the user ie. a similar page on the new site else I will send to the home page.


回答1:


That's right, just do it in your routes configuration (usually in global.asax). You can set these up as static special cases.

routes.MapRoute("Page3", 
            "SomeURL/Page3.html",
            new { 
                  controller = "SomeController",
                  action = "SomeAction",
                  page = "2"
                });



回答2:


For PageXX.html, PageX.html, Index.html pages you can do regular expression based matching too. That will allow you to maintain the whole thing with a single route mapping.



来源:https://stackoverflow.com/questions/1650882/asp-net-mvc-trapping-certain-urls-to-do-301-redirect

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