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

被刻印的时光 ゝ 提交于 2019-12-01 19:59:18
Max Ivak

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/

Steve_333

Not sure if you still need this, but you could solve this with a dirty solution:

Use HttpContext.Current.Request.MapPath("") to get the physical location of the route, and then loop over all aspx files (and anything else you want to map) using the DirectoryInfo/FileInfo classes. You can then dynamically register alternative paths for your files.

I've done a prototype and it appears to be working, although of course, the devil is always in the details.

Cheers,

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