【问题标题】:custom mapRoute for special controller and action to be like this in MVC: http://example.com/someValue特殊控制器的自定义 mapRoute 和动作在 MVC 中是这样的:http://example.com/someValue
【发布时间】:2015-07-29 09:42:07
【问题描述】:

我有这个默认的 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.Actions 都有 "Index" 作为动作,例如 @Url.Action("index","login") 不工作, 我也用过:

sub=UrlParameter.Optional

sub=""

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

【问题讨论】:

    标签: c# asp.net-mvc asp.net-routing maproute


    【解决方案1】:

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

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

    【讨论】:

    • 很抱歉在这里发表评论,但请您看看这个问题:link
    【解决方案2】:

    Sam 的回答很好,但我会反其道而行之。如果“someValue”的参数 {sub} 是动态的(存储在数据库中),我将创建一个排除现有控制器的约束。所以如果你打电话

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-31
      • 1970-01-01
      • 2015-10-12
      相关资源
      最近更新 更多