加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 在asp.net mvc项目中路由传统的asp.net链接

发布时间:2020-12-15 08:11:45 所属栏目:百科 来源:网络整理
导读:我有一段时间需要在我的asp.net mvc项目中支持以下url. http://www.example.com/d.aspx?did=1234 我需要将此映射到此URL. http://www.example.com/Dispute/Detail/1234 我已经查看了以下信息. http://blog.eworldui.net/post/2008/04/ASPNET-MVC—Legacy-Url
我有一段时间需要在我的asp.net mvc项目中支持以下url.
http://www.example.com/d.aspx?did=1234

我需要将此映射到此URL.

http://www.example.com/Dispute/Detail/1234

我已经查看了以下信息.

http://blog.eworldui.net/post/2008/04/ASPNET-MVC—Legacy-Url-Routing.aspx

ASP.Net MVC routing legacy URLs passing querystring Ids to controller actions

在尝试遵循这一点时,我可以获得第一个工作网址,但之后所有其他网址都被破坏了.谁能看到我出错的地方?

这是我的路线.

public static void RegisterRoutes(RouteCollection routes)
    {
        // all my other routes

        // Legacy routes
        routes.Add(
          "Legacy",new LegacyRoute(
            "d.aspx","LegacyDirectDispute",new LegacyRouteHandler())
        );

        routes.MapRoute(
          "LegacyDirectDispute","Dispute/Details/{id}",new { controller = "Dispute",action = "Details",id = "" }
        );

        routes.MapRoute(
          "Default",// Route name
          "{controller}/{action}/{id}",// URL with parameters
          new { controller = "Home",action = "Index",id = "" }  // Parameter defaults
        );
    }

这是我正在使用的global.asax.cs中的代码.

public class LegacyRoute : Route
 {
  public string RedirectActionName { get; set; }
  public LegacyRoute(string url,string redirectActionName,IRouteHandler routeHandler)
   : base(url,routeHandler)
  {
   RedirectActionName = redirectActionName;
  }
 }

 // The legacy route handler,used for getting the HttpHandler for the request
 public class LegacyRouteHandler : IRouteHandler
 {
  public IHttpHandler GetHttpHandler(RequestContext requestContext) 
  {
   return new LegacyHandler(requestContext);
  }
 }

 // The legacy HttpHandler that handles the request
 public class LegacyHandler : MvcHandler
 {
  private RequestContext requestContext;
  public LegacyHandler(RequestContext requestContext)
   : base(requestContext)
  {
   this.requestContext = requestContext;
  }

  protected override void ProcessRequest(HttpContextBase httpContext)
  {
   string redirectActionName = ((LegacyRoute)RequestContext.RouteData.Route).RedirectActionName;

   var queryString = requestContext.HttpContext.Request.QueryString;
   foreach (var key in queryString.AllKeys)
   {
    if (key.Equals("did"))
    {
     requestContext.RouteData.Values.Add("id",queryString["did"]);
    }
    else
    {
     requestContext.RouteData.Values.Add(key,queryString[key]);
    }
   }

   VirtualPathData path = RouteTable.Routes.GetVirtualPath(requestContext,redirectActionName,requestContext.RouteData.Values);

   if (path != null)
   {
    httpContext.Response.Status = "301 Moved Permanently";
    httpContext.Response.AppendHeader("Location",path.VirtualPath);
   }
  }
 }

解决方法

您可以在站点根目录中创建一个名为d.aspx的文件,其内容类似于以下内容:
<script runat="server">
protected void Page_Load(object sender,EventArgs e)
{
    Response.Redirect(string.Format("http://{0}/Dispute/Detail/{1}",Request.Url.Host,Request.QueryString.Get("did")));
}
</script>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读