mvc5动态路由和指向它的Html.ActionLink

时间:2017-02-11 19:18:48

标签: routing asp.net-mvc-5 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() }
            );

我希望超级链接像“/ kind / Trip”

当我把它变成

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

它给我的网址像

HREF = “/主页/种类国家= Egypt_Holiday&安培;跳闸= Classical_Tours”

我想要

href =“Egypt_Holiday / Classical_Tours”

请帮助

1 个答案:

答案 0 :(得分:0)

尝试在URL路由配置中添加country和trip参数。

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