簡體   English   中英

MVC 4 URL路由以吸收舊的舊URL並轉發到新域

[英]MVC 4 URL routing to absorb old legacy urls and forward to new domain

我的域曾經指向一個wordpress網站,在該網站上我使用以下格式設置了特定頁面:

www.mydomain.com/product/awesome-thing
www.mydomain.com/product/another-thing

最近,我轉移了我的域名,現在它指向了我網站的MVC版本。 上面提到的鏈接不再有效,但是wordpress網站仍然存在其他域。 我正在嘗試讓我的mvc網站吸收以前的鏈接並將其轉發給

http://mydomain.wordpress.com/product/awesome-thing 
http://mydomain.wordpress.com/product/another-thing

我現在所擁有的是RouteConfig.cs的以下RouteConfig.cs

routes.MapRoute(
            name: "product",
            url: "product/{id}",
            defaults: new { controller = "product", action = "redirect", id = UrlParameter.Optional });

在我的產品控制器中,我有以下內容

public void redirect(string id)
{
   if (id == "awesome-thing")
        {
            Response.Redirect("http://mydomain.wordpress.com/product/awesome-thing ");
        }
        if (id == "another-thing")
        {
            Response.Redirect("http://mydomain.wordpress.com/product/another-thing");
        }
        Response.Redirect(" http://mydomain.wordpress.com/");
}

但是, RouteConfig.cs路由未與控制器正確鏈接。 我不斷收到“ 404無法找到資源”錯誤。

我設法通過重新排序地圖路線來解決此問題。 我還更改了控制器和maproute中的代碼,下面的代碼最終起作用了。

routes.MapRoute(
          name: "productAwesome",
          url: "product/awesome-thing",
          defaults: new { controller = "product", action = "redirectAwsome" });

routes.MapRoute(
         name: "productAnother",
         url: "product/another-thing",
         defaults: new { controller = "product", action = "redirectAnother" });

//it's important to have the overriding routes before the default definition. 
routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );

然后在產品控制器中添加以下內容:

public class productController : Controller
{

    public void redirectAwsome()
    {
        Response.Redirect("http://mydomain.wordpress.com/product/awesome-thing ");
    }
    public void redirectAnother()
    {
        Response.Redirect("http://mydomain.wordpress.com/product/another-thing");
    }
}

暫無
暫無

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

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