【问题标题】:How to redirect custom aspx page to mvc action method如何将自定义 aspx 页面重定向到 mvc 操作方法
【发布时间】:2016-10-07 06:52:46
【问题描述】:

我已将 aspx 项目升级到 mvc。现在我的一些老客户使用 .aspx 页面调用 url,他们在 mvc 项目中得到 404(未找到)。

所以现在我必须将 .aspx 重定向到 mvc 页面。

旧网址

www.domain.com/bookshop/showproduct.aspx?isbn=978-1-59333-934-0

新网址

www.domain.com/{product_name}

我想通过mvc的路由机制来做。就像一旦出现这种类型的 url 那么它应该调用我的自定义 mvc 操作并在字符串参数中我将得到 showproduct.aspx?isbn=978-1-59333-934-0

您能否建议一种使用最少代码的最佳方法。

【问题讨论】:

    标签: c# asp.net asp.net-mvc routing nopcommerce


    【解决方案1】:

    新建一个RouteHandler类,如下图:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Routing;
    
    namespace Sample.Helpers
    {
        public class RouteHandler : IRouteHandler
        {
            public IHttpHandler GetHttpHandler(RequestContext requestContext)
            {
                return new ASPDotNetHttpHandler();
            }
        }
    
        public class ASPDotNetHttpHandler : IHttpHandler
        {
            public bool IsReusable
            {
                get
                {
                    return true;
                }
            }
    
            public void ProcessRequest(HttpContext context)
            {
                string product = context.Request.QueryString["isbn"];
                int index = context.Request.Url.AbsoluteUri.IndexOf("bookshop/showproduct.aspx?");
    
                if (!(string.IsNullOrEmpty(product) || index == -1))
                {
                    string newUrl = context.Request.Url.AbsoluteUri.Substring(0, index)+"/" + product;
                    context.Response.Redirect(newUrl, true);
                }
            }
        }
    }
    

    在RouteConfig.cs文件的RegisterRoutes方法中插入如下所示的新路由:

        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    
        routes.Add(new Route("bookshop/showproduct.aspx", new BIRS.Web.Helpers.RouteHandler()));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-03
      • 2012-10-10
      • 1970-01-01
      • 1970-01-01
      • 2021-02-21
      • 1970-01-01
      • 2014-10-29
      • 2010-11-08
      相关资源
      最近更新 更多