mvc5动态路由和Html.ActionLink指着它
问题描述:
这个动态路由功能工作正常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”
请帮助
答
尝试增加国家和跳闸参数在你的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() }
);
我的推杆[路线(“{国家}/{行程} /”)]在我的控制器类直接路由到它,因为我想,但它的路线到每一个Java脚本和CSS文件 –
我需要立即帮助请如果你知道任何一个如何可以帮助Facebook或其他地方或标记他 –