簡體   English   中英

如何將自定義 aspx 頁面重定向到 mvc 操作方法

[英]How to redirect custom aspx page to mvc action method

我已將 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

您能否建議一種使用最少代碼執行此操作的最佳方法。

創建一個新類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()));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM