如何从路由表中删除路由

时间:2014-11-24 06:00:52

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

我正在尝试从路由表中删除路由。我的代码是:

var pages = from v in db.tblCmsPages where v.IsActive select v.PageUrlName;
foreach (string row in pages)
{
       routes.MapRoute(
       name: "mypages_"+row,
       url: row,
       defaults: new { controller = "MyPages", action = "en" },
       namespaces: new[] { "MGP_RealState.Controllers" }
       );
}

这是我的路线收藏。我只是想从这个集合中删除一个特定的路线。 我尝试使用linq选择特定路线,就像

一样
var myrouter = from v in System.Web.Routing.RouteTable.Routes where v.? select v

不工作

System.Web.Routing.RouteTable.Routes.Remove(?);

1 个答案:

答案 0 :(得分:0)

我们需要创建一个路由对象。

//Create the object of particular router
var rr = new System.Web.Routing.Route(tblcmspage.PageUrlName, new MvcRouteHandler())
            {
                Defaults = new System.Web.Routing.RouteValueDictionary(new { controller = "MyPages", action = "en" }),
                DataTokens = new System.Web.Routing.RouteValueDictionary(new { namespaces = new[] { "MGP_RealState.Controllers" } })
            };

//delete the router                
System.Web.Routing.RouteTable.Routes.Remove(rr);