RedirectToPage和RedirectToPagePermanent无法正常工作

时间:2019-05-13 22:07:09

标签: c# asp.net .net-core url-rewriting

我有一个DotnetCore 2.1站点,我希望使用数据库映射新页面将旧的Webforms ASPX页面甚至旧的经典ASP页面重定向到各自的页面。 例如,有一个对mywebsite.com/factoryclearancesale.aspx的请求,我想将其重定向到mywebsite.com/factory-clearance 同样,如果有对mywebsite.com/pressreleases.asp的请求,则它将重定向到mywebsite.com/press-releases 基本上,该请求被拦截,完成数据库查找以获取新的对应页面,然后在控制器中使用RedirectToPagePermanent(newPageName)。 听起来做起来很简单(在过去,response.redirect可以正常工作),但是,结果(而不是重定向到正确的页面)(对于第一个示例)为: mywebsite.com/?page=%2工厂清除

由于我没有路由,所以我认为此方法可以工作,但是它将“ page =”注入到URI中,这没有帮助。 有什么办法可以正确地做到这一点? 这是完整的方法(我对他人代码的更改在底部)

public virtual IActionResult GeneralRedirect()
{
    // use Request.RawUrl, for instance to parse out what was invoked
    // this regex will extract anything between a "/" and a ".aspx"
    var regex = new Regex(@"(?<=/).+(?=\.aspx)", RegexOptions.Compiled);
    var rawUrl = _webHelper.GetRawUrl(this.HttpContext.Request);
    var aspxfileName = regex.Match(rawUrl).Value.ToLowerInvariant();

    switch (aspxfileName)
    {
        //URL without rewriting
        case "product":
            {
                return RedirectProduct(_webHelper.QueryString<string>("productid"), false);
            }
        case "category":
            {
                return RedirectCategory(_webHelper.QueryString<string>("categoryid"), false);
            }
        case "manufacturer":
            {
                return RedirectManufacturer(_webHelper.QueryString<string>("manufacturerid"), false);
            }
        case "producttag":
            {
                return RedirectProductTag(_webHelper.QueryString<string>("tagid"), false);
            }
        case "news":
            {
                return RedirectNewsItem(_webHelper.QueryString<string>("newsid"), false);
            }
        case "blog":
            {
                return RedirectBlogPost(_webHelper.QueryString<string>("blogpostid"), false);
            }
        case "topic":
            {
                return RedirectTopic(_webHelper.QueryString<string>("topicid"), false);
            }
        case "profile":
            {
                return RedirectUserProfile(_webHelper.QueryString<string>("UserId"));
            }
        case "compareproducts":
            {
                return RedirectToRoutePermanent("CompareProducts");
            }
        case "contactus":
            {
                return RedirectToRoutePermanent("ContactUs");
            }
        case "passwordrecovery":
            {
                return RedirectToRoutePermanent("PasswordRecovery");
            }
        case "login":
            {
                return RedirectToRoutePermanent("Login");
            }
        case "register":
            {
                return RedirectToRoutePermanent("Register");
            }
        case "newsarchive":
            {
                return RedirectToRoutePermanent("NewsArchive");
            }
        case "search":
            {
                return RedirectToRoutePermanent("ProductSearch");
            }
        case "sitemap":
            {
                return RedirectToRoutePermanent("Sitemap");
            }
        case "recentlyaddedproducts":
            {
                return RedirectToRoutePermanent("NewProducts");
            }
        case "shoppingcart":
            {
                return RedirectToRoutePermanent("ShoppingCart");
            }
        case "wishlist":
            {
                return RedirectToRoutePermanent("Wishlist");
            }
        case "CheckGiftCardBalance":
            {
                return RedirectToRoutePermanent("CheckGiftCardBalance");
            }
        default:
            break;
    }

    //Code to redirect old pages to new ones
    string oldURI = rawUrl.TrimStart(Convert.ToChar(@"/"));
    string newURI = Nop.Web.Custom.MyData.GetNewURIFromOldURI(oldURI);
    if (newURI.Length > 0)
    {
        return RedirectToPagePermanent(newURI); //This injects ?page= into the new URI - not expected, so must be doing wrong!!!!
    }

    //no permanent redirect in this case
    return RedirectToRoute("HomePage");
}

0 个答案:

没有答案