2013-10-25 37 views
0

我用來指向一個WordPress網站,我已經使用以下格式設置特定的網頁域名:MVC 4 URL路由吸收舊舊的URL,並轉發到新域名

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

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找不到資源」錯誤。

回答

0

我設法通過重新排序我的地圖路線來解決這個問題。我也改變了控制器和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 } 
     ); 

然後在產品控制器I加入下列:

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"); 
    } 
}