特殊控制器和动作的自定义 mapRoute 在 MVC 中是这样的:http://example.com/someValue

custom mapRoute for special controller and action to be like this in MVC: http://example.com/someValue

我有这个默认的地图路线:

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

和一些其他 mapRoutes 在默认之前用于其他一些 sontroller,

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

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

我所有的 url.Actions 都有 "Index" 作为 @Url.Action("index","login") 不行, 我也用过:

sub=UrlParameter.Optional

sub=""

,但他们没有工作, 我应该怎么办 ?

您的 categories 路由捕获所有 url,因为它与角色匹配。例如,您需要编写一个自定义约束 class 来从您的数据库中过滤掉不匹配的 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} for "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