2017-02-11 85 views
0

這個動態路由功能工作正常mvc5動態路由和Html.ActionLink指着它

public class CategoryRouteConstraint : IRouteConstraint 
{ 
    public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection) 
    { 
     //these would usually come from a database, or cache. 
     FlyingCarpetMVC.Models.MyDbContext db = new FlyingCarpetMVC.Models.MyDbContext(); 
     var categories = db.Country_name.Select(x => new {x.English_name,x.Spanish_name}).ToList(); 

     if (values[parameterName] == null) 
      return false; 

     //get the category passed in to the route 
     var category = values[parameterName].ToString(); 
     category = category.Replace("-", " "); 
     category = category.Replace("_", " "); 
     //now we check our categories, and see if it exists 
     bool Exist = categories.Any(x => x.English_name.Contains(category) || x.Spanish_name.Contains(category)); 
     return Exist; 
     // url such as /restaurants/Camberley--Surrey will match 
     // url such as /pubs/Camberley--Surrey will not 
    } 
} 

routes.MapRoute(
      name: "CategoryRoute", 
      url: "{category}/{location}", 
      defaults: new { controller = "Home", action = "kind", Country = UrlParameter.Optional,trip=UrlParameter.Optional }, 
      constraints: new { category = new CategoryRouteConstraint() } 
      ); 

我想要的超臨客像「/樣/旅行」

當我使它像

@Html.ActionLink(MyThisTripName,"kind", new {Kind=CountryNam,Trip= MyThisTripName },null) 

它給了我的url像

HREF =「/首頁/樣?國家= Egypt_Holiday &行程= Classical_Tours」

我想

HREF = 「Egypt_Holiday/Classical_Tours」

請幫助

回答

0

嘗試增加國家和跳閘參數在你的URL路由配置。

routes.MapRoute(
      name: "CategoryRoute", 
      url: "{category}/{location}/{Country}/{Url}", 
      defaults: new { controller = "Home", action = "kind", Country = UrlParameter.Optional,trip=UrlParameter.Optional }, 
      constraints: new { category = new CategoryRouteConstraint() } 
      ); 
+0

我的推杆[路線(「{國家}/{行程} /」)]在我的控制器類直接路由到它,因爲我想,但它的路線到每一個Java腳本和CSS文件 –

+0

我需要立即幫助請如果你知道任何一個如何可以幫助Facebook或其他地方或標記他 –