Wildcards with ASP.NET MVC MapPageRoute to support organizing legacy code

前端 未结 2 645
梦如初夏
梦如初夏 2021-01-19 08:10

I\'m working on migrating an existing ASP.NET web site into an MVC project. There are several (60+) pages that I don\'t want to rewrite just yet, and so I\'m wondering if th

2条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-19 08:44

    I solved this using a controller which returns the contents of the file. It is not a perfect and fast solution but it works.

    routes.MapRoute(
        "legacyroutes",
        "{filename}.aspx",
        new { controller = "Home", action = "RedirectFile"}
    ); 
    

    And the controller:

    public class HomeController : Controller
    {
        public ActionResult RedirectFile(string filename)
        {
            string url = Url.Content("~/Legacy/"+filename+".aspx");
            Redirect(url); // or other code to process the file
        }
    }
    

    Details and other examples here: http://maxivak.com/dynamic-url-rewriting-on-asp-net-mvc/

提交回复
热议问题