在MVC中为特殊的控制器和动作定制mapRoute: http://example.com/someValue

本文关键字:mapRoute http com example someValue MVC 控制器 | 更新日期: 2023-09-27 18:05:13

我有这个默认的mapRoute:

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

和其他一些mapRoutes在默认情况下,

现在,我想要一个特殊控制器的mapRoute来显示url,如:myDomain.com/someValue,但是当我使用这个:

routes.MapRoute(
name: "categories",
url: "{sub}",
defaults: new { controller = "cat", action = "Index" }
);

所有我的url。动作"Index"作为动作,如@Url.Action(" Index","login")不工作;我也用过:

子= UrlParameter。可选

子= " "

,但它们不起作用,我该怎么办?

在MVC中为特殊的控制器和动作定制mapRoute: http://example.com/someValue

您的categories路由捕获所有url,因为它与角色匹配。例如,您需要编写一个自定义约束类来从数据库中过滤出不匹配的sub

public class MyCatConstraint : IRouteConstraint
{
    // suppose this is your cats list. In the real world a DB provider 
    private string[] _myCats = new[] { "cat1", "cat2", "cat3" };
    public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
    {
        // return true if you found a match on your cat's list otherwise false
        // in the real world you could query from DB to match cats instead of searching from the array.  
        if(values.ContainsKey(parameterName))
        {
            return _myCats.Any(c => c == values[parameterName].ToString());
        }
        return false;
    }
}

,然后把这个约束添加到你的路由中。

routes.MapRoute(
    name: "categories",
    url: "{sub}",
    defaults: new { controller = "cat", action = "Index" }
    ,constraints: new { sub = new MyCatConstraint() }
    );

Sam的答案很好,但我想用另一种方式来回答。如果你的参数{sub}为"someValue"是动态的(存储在数据库中),我会创建一个排除你现有的控制器的约束。所以如果你调用

domain.com/homedomain.com/contact

它将到达这些控制器,否则将通过类别路由。例如:
public class NotEqual : IRouteConstraint
{
    private string[] _match = null;
    public NotEqual(string[] match)
    {
        _match = match;
    }
    public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
    {
        foreach(var controllername in _match)
        {
            if (String.Compare(values[parameterName].ToString(), controllername, true) == 0)
                return false;
        }
        return true;
    }
}

修改版本:http://stephenwalther.com/archive/2008/08/07/asp-net-mvc-tip-30-create-custom-route-constraints

,你的路由将是:

        routes.MapRoute(
            name: "categories",
            url: "{sub}",
            defaults: new { controller = "cat", action = "Index" }
            , constraints: new { sub = new NotEqual(new string[] { "Contact", "Home" }) }
         );
        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        );

这样,你输入的与domain.com/contactdomain.com/home不同的内容将被重新路由到cat控制器

@Url.Action("Index","Contact") 

应该产生:

/Contact