【问题标题】:How to build routes only to action and to controller with id?如何构建仅用于操作和带有 id 的控制器的路由?
【发布时间】:2018-09-27 11:59:27
【问题描述】:

我正在尝试构建两条路线,仅用于操作和带有 id 的控制器,保持默认。

我必须访问:

  1. www.mysite.com/MyController/MyAction/{OptionalId}
  2. www.mysite.com/MyController/{OptionalId}
  3. www.mysite.com/MyActionFromHomeController

我能够创建路线来处理第一点和第三点,但不能用于第二点。当前代码:

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

        routes.MapRoute(
            name: "OnlyActionToHomeController",
            url: "{action}",
            defaults: new { controller = "Home" },
            constraints: new { noConflictingControllerExists = new NoConflictingControllerExists() }
        );

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

public class NoConflictingControllerExists : IRouteConstraint
{
    private static readonly Dictionary<string, bool> _cache = new Dictionary<string, bool>(StringComparer.OrdinalIgnoreCase);

    public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
    {
        var path = httpContext.Request.Path;

        if (path == "/" || String.IsNullOrEmpty(path))
            return false;

        if (_cache.ContainsKey(path))
            return _cache[path];

        IController ctrl;

        try
        {
            var ctrlFactory = ControllerBuilder.Current.GetControllerFactory();
            ctrl = ctrlFactory.CreateController(httpContext.Request.RequestContext, values["action"] as string);
        }
        catch
        {
            _cache.Add(path, true);
            return true;
        }

        var res = ctrl == null;
        _cache.Add(path, res);

        return res;
    }
}

【问题讨论】:

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


    【解决方案1】:

    我做到了!

    public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    
            routes.MapRoute(
                name: "OnlyController",
                url: "{controller}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
                constraints: new { id = @"\d+" } // define the id parameter needs to be integer
            );
    
            routes.MapRoute(
                name: "OnlyActionToHomeController",
                url: "{action}",
                defaults: new { controller = "Home" },
                constraints: new { noConflictingControllerExists = new NoConflictingControllerExists() }
            );
    
            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        }
    }
    
    public class NoConflictingControllerExists : IRouteConstraint
    {
        private static readonly Dictionary<string, bool> _cache = new Dictionary<string, bool>(StringComparer.OrdinalIgnoreCase);
    
        public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
        {
            var path = httpContext.Request.Path;
    
            if (path == "/" || String.IsNullOrEmpty(path))
                return false;
    
            if (_cache.ContainsKey(path))
                return _cache[path];
    
            IController ctrl;
    
            try
            {
                var ctrlFactory = ControllerBuilder.Current.GetControllerFactory();
                ctrl = ctrlFactory.CreateController(httpContext.Request.RequestContext, values["action"] as string);
            }
            catch
            {
                _cache.Add(path, true);
                return true;
            }
    
            var res = ctrl == null;
            _cache.Add(path, res);
    
            return res;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-02
      • 1970-01-01
      • 1970-01-01
      • 2021-05-31
      • 1970-01-01
      • 2016-04-03
      • 1970-01-01
      • 2017-03-09
      相关资源
      最近更新 更多