ASP.NET MVC MapPageRoute 的通配符支持组织遗留代码

发布于 2024-09-11 15:41:57 字数 628 浏览 6 评论 0原文

我正在致力于将现有的 ASP.NET 网站迁移到 MVC 项目中。有几个(60+)页面我还不想重写,所以我想知道是否有一种方法可以:

  • 将现有的 .aspx 页面(标记和代码隐藏文件)移动到我的 MVC 结构中的“Legacy”文件夹
  • 设置路由,以便对 /foo.aspx(不带“legacy”)的调用实际上会调用 ~/Legacy/foo.aspx

实际上,我不希望 URL 中出现“legacy”,但是我也不希望 MVC 解决方案充满旧版 .aspx 页面。我承认这是一个非常小的问题,我只是好奇是否可以通过路由来完成。

我意识到我可以这样做:

routes.MapPageRoute("legacy-foo", "Foo.aspx", "~/Legacy/Foo.aspx"); 

但我想知道是否有办法动态地做到这一点(使用 MVC 路由)?例如:

routes.MapPageRoute("legacyroutes", "{filename}.aspx", "~/Legacy/{filename}.aspx"); 

我想一种方法是使用 URL 重写器模块,但如果路由本身能够执行此操作,那么这似乎有点多余。

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 there's a way that I can:

  • Move the existing .aspx pages (both markup and code-behind files) into a 'Legacy' folder in my MVC structure
  • Set up routing so a call to /foo.aspx (without 'legacy') will actually invoke ~/Legacy/foo.aspx

Effectively, I don't want "legacy" in the URL, but I also don't want the MVC solution to be full of legacy .aspx pages. I accept that this is a very minor point, I'm just curious if it can be done with Routing.

I realize I could do:

routes.MapPageRoute("legacy-foo", "Foo.aspx", "~/Legacy/Foo.aspx"); 

but I'm wondering if there is a way to do that dynamically (using MVC routes)? eg:

routes.MapPageRoute("legacyroutes", "{filename}.aspx", "~/Legacy/{filename}.aspx"); 

I guess one way is to use a URL rewriter module, but that seems a bit redundant if routes is capable of doing this natively.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

往事风中埋 2024-09-18 15:41:57

我使用返回文件内容的控制器解决了这个问题。这不是一个完美且快速的解决方案,但它确实有效。

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

和控制器:

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
    }
}

详细信息和其他示例在这里:http://maxivak。 com/dynamic-url-rewriting-on-asp-net-mvc/

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/

流殇 2024-09-18 15:41:57

不确定您是否仍然需要这个,但是您可以用一个肮脏的解决方案来解决这个问题:

使用 HttpContext.Current.Request.MapPath("") 获取路线的物理位置,然后循环使用 DirectoryInfo/FileInfo 类的所有 aspx 文件(以及您想要映射的其他任何内容)。然后,您可以动态注册文件的替代路径。

我已经完成了一个原型,它看起来很有效,尽管当然,细节总是决定成败。

干杯,

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,

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文