【问题标题】:Maintain URL when going from ASP.NET WebForms to ASP.NET MVC从 ASP.NET WebForms 到 ASP.NET MVC 时维护 URL
【发布时间】:2013-01-01 14:57:20
【问题描述】:

我正在将旧的 ASP.NET WebForms 应用程序转换为 ASP.NET MVC 4。一切都很好,除了我需要保持与特定 URL 的向后兼容性。我找到了this great post on using UrlRewrite,但遗憾的是这不是我可以指望的(这个应用程序被部署到很多服务器上)。在底部,他提到如果您只有一小部分 URL 需要处理,则使用路由,但没有提供任何示例。

由于我只有一个 url 要处理,我认为路由是一种简单的方法,但除了默认路由 /Controller/Action/{id} 之外,我从未处理过任何事情,所以我正在寻找一种解决方案

  1. 没有外部依赖项
  2. 可以在旧的蹩脚浏览器上运行
  3. 我的应用是否知道这个旧网址并不重要

旧的 URI

https://www.mysite.com/default.aspx?parm1=p1&parm2=p2&etc=soforth

新的 URI

https://www.mysite.com/Home/Index/?parm1=p1&parm2=p2&etc=soforth

背景:这个应用程序被部署到不同位置的许多服务器上。还有其他应用程序(我无法更新)在 Web 浏览器控件中显示“旧 URI”,因此我需要它们在应用程序更新到 asp.net mvc 后继续工作。

【问题讨论】:

  • 为什么不在您的项目的web.config中添加一个重写规则,如您的链接所示?
  • 你看过MapPageRoute()吗?
  • @cheesemacfly 我不能指望 url 重写模块在我将部署到的所有服务器上都处于活动状态。

标签: c# asp.net-mvc url-rewriting asp.net-mvc-routing url-routing


【解决方案1】:

类似下面的东西应该可以工作(未经测试,可能需要使这条路线成为第一条路线之一):

routes.MapRoute(
   "legacyDefaultPage",
   "default.aspx",
   new {Controller = "Legacy", Action="Default"});

class LegacyController {
  ActionResult Default (string param1,...){}
}

【讨论】:

    【解决方案2】:

    您可以尝试创建一个 httpModule,它会在点击您的 MVC 应用程序中的任何路由之前执行。

    如果 seo 很重要,您可以在您的 http 模块中执行 301 or 302 redirection,这将使您更灵活地将所有参数从旧应用程序转换为新应用程序。

    public class RecirectionModule :IHttpModule{
        public void Init(HttpApplication context)
        {
            _context = context;
            context.BeginRequest += OnBeginRequest;
        }
    
        public void OnBeginRequest(object sender, EventArgs e)
        {
            string currentUrl = HttpContext.Current.Request.Url.AbsoluteUri;
            string fileExtention = Path.GetExtension(currentUrl);
            string[] fileList= new[]{".jpg",".css",".gif",".png",".js"};
    
            if (fileList.Contains(fileExtention)) return;
    
            currentUrl = DoAnyTranformation(currentUrl);
            Redirect(currentUrl);
    
        }
    
        private void Redirect(string virtualPath)
        {
            if (string.IsNullOrEmpty(virtualPath)) return;
            _context.Context.Response.Status = "301 Moved Permanently";
            _context.Context.Response.StatusCode = 301;
            _context.Context.Response.AppendHeader("Location", virtualPath);
            _context.Context.Response.End();
        }
    
        public void Dispose()
        {
    
        }
    
    }
    

    我认为,如果您需要更改新应用程序中的参数列表并且处理大量路由很快就会变得丑陋,那么执行重定向可能是一种更简洁的解决方案。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-06
      • 2011-01-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多