MVC 4 URL路由以吸收旧的旧URL并转发到新域

时间:2013-10-25 19:41:37

标签: c# asp.net-mvc asp.net-mvc-4

我的域名曾经指向一个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

中的以下内容
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无法找到资源”的错误。

1 个答案:

答案 0 :(得分: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 }
        );

然后在产品控制器中我添加了以下内容:

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