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

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

概述我有一段时间需要在我的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-Ur 我有一段时间需要在我的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>
总结

以上是内存溢出为你收集整理的c# – 在asp.net mvc项目中路由传统的asp.net链接全部内容,希望文章能够帮你解决c# – 在asp.net mvc项目中路由传统的asp.net链接所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1247415.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-07
下一篇 2022-06-07

发表评论

登录后才能评论

评论列表(0条)

保存