@ html.ActionLink用于自定义路由

时间:2015-02-14 06:59:07

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

我想要为我的应用程序提供以下类型的URL

http://domaiinname/Conent

所以我创建了自定义路线并以某种口音实现了这一点。但现在的问题是如何从ActionLink生成该URL。

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
           name: "Custom",
           url: "{content}",
           defaults: new { controller = "Content", action = "Index" },
           namespaces: new[] { "Some.Web.Controllers" },
           constraints: new { IsContent = new IsContentPage() }
       );           

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
            namespaces: new[] { "Some.Web.Controllers" }
        );

    }
}

网址约束

 public class IsContentPage : IRouteConstraint
{
    public bool Match
          (
              HttpContextBase httpContext,
              Route route,
              string parameterName,
              RouteValueDictionary values,
              RouteDirection routeDirection
          )
    {
        var enumlist = Enum.GetValues(typeof(Common.Content)).Cast<Common.Content>().Select(v => v.ToString().ToLower()).ToList();
        var keysValue = values.Select(x => x.Value.ToString().ToLower()).FirstOrDefault();
        Int32 isContent = enumlist.Where(x => x == keysValue).Count();
        if (isContent > 0)
        {
            return true;
        }
        return false;
    }
}

现在我如何生成 http://domain/content

等链接

1 个答案:

答案 0 :(得分:0)

尝试:

@Html.ActionLink("My Custom Content", "Index", "Content")