Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc MVC3路由约束参数之间的区别_Asp.net Mvc_Asp.net Mvc 3_Asp.net Mvc Routing - Fatal编程技术网

Asp.net mvc MVC3路由约束参数之间的区别

Asp.net mvc MVC3路由约束参数之间的区别,asp.net-mvc,asp.net-mvc-3,asp.net-mvc-routing,Asp.net Mvc,Asp.net Mvc 3,Asp.net Mvc Routing,对于我的网站,我正在从xml(-nodes)动态建立链接。大多数情况下,productdetails的链接都是这样建立的: me.com/one/two/three/four/productdetail 但在某些情况下,在第四个节点之后有第五个节点,因此链接为: me.com/one/two/three/four/five/productdetail 在我的操作方法中,如果第五个“段”和productdetail都是字符串,我如何区分它们 公共ActionResult产品(字符串1、字符串2、字

对于我的网站,我正在从xml(-nodes)动态建立链接。大多数情况下,productdetails的链接都是这样建立的:

me.com/one/two/three/four/productdetail

但在某些情况下,在第四个节点之后有第五个节点,因此链接为:

me.com/one/two/three/four/five/productdetail

在我的操作方法中,如果第五个“段”和productdetail都是字符串,我如何区分它们

公共ActionResult产品(字符串1、字符串2、字符串3、字符串4、字符串productdetail、字符串5=null) { }

我现在有如下定义的路线:

{controller}/{action}/{one}/{two}/{three}/{four}/{productdetail}

{controller}/{action}/{one}/{two}/{three}/{four}/{five}/{productdetail}

(其中{one}到{five}是urlparmeter.Optional)

带有正则表达式的路由约束似乎没有帮助,因为它们都是字符串(甚至可以是非常相似的字符串)


这可能吗?

通过有两条单独的路线,一条有五个元素,另一条有四个元素,这些路线可以到达同一个地方。一个通过{5},另一个不通过


编辑:如果两个模式都相同,那么唯一的方法就是使用约束来区分不同的值。

通过使用两个单独的路由,一个包含五个元素,另一个包含四个元素,这些路由可以到达相同的位置。一个通过{5},另一个不通过


编辑:如果两种模式相同,那么唯一的方法就是使用约束来区分不同的值。

我认为您可以对路由声明进行一些更改以满足您的期望

  • 不要把1到5作为可选参数
  • MapRoot{controller}/{action}/{one}/{two}/{two}/{two}/{two}/{two}/{two}/{two}/{two}/{two}/{two}/{two}/{two}/{productdetail}应该在{controller}/{action}之前
  • 后跟默认映射路由。{controller}/{action}/{id}id是可选的
  • 现在

  • 对me.com/one/two/three/four/five/productdetail的请求将匹配 只有第一条路
  • 对me.com/one/two/three/four/productdetail的请求将匹配 只有第二条路

  • 我认为您可以对路线声明进行一些更改,以满足您的期望

  • 不要把1到5作为可选参数
  • MapRoot{controller}/{action}/{one}/{two}/{two}/{two}/{two}/{two}/{two}/{two}/{two}/{two}/{two}/{two}/{two}/{productdetail}应该在{controller}/{action}之前
  • 后跟默认映射路由。{controller}/{action}/{id}id是可选的
  • 现在

  • 对me.com/one/two/three/four/five/productdetail的请求将匹配 只有第一条路
  • 对me.com/one/two/three/four/productdetail的请求将匹配 只有第二条路

  • 如果在您的情况下,您无法设置路由约束,那么最好的选择是稍微更改路由,以便ASP.NET MVC可以确定哪一个是您的产品详细信息参数,以避免与可选的{five}混淆

    在您的产品详细信息前面加上一些特定字符串,这些字符串在一个值到五个值中都不会出现,例如:

    {controller}/{action}/{one}/{two}/{three}/{four}/p/{productdetail}
    
    {controller}/{action}/{one}/{two}/{three}/{four}/{five}/p/{productdetail}
    

    这将解决您的问题。

    如果您无法设置路由约束,那么您最好的选择是更改一点路由,以便ASP.NET MVC可以确定哪一个是您的产品详细信息参数,以避免与可选的{5}混淆

    在您的产品详细信息前面加上一些特定字符串,这些字符串在一个值到五个值中都不会出现,例如:

    {controller}/{action}/{one}/{two}/{three}/{four}/p/{productdetail}
    
    {controller}/{action}/{one}/{two}/{three}/{four}/{five}/p/{productdetail}
    

    这将解决您的问题。

    解决此问题的最简单方法是为5、4、3和2参数注册路由。按顺序登记

     {controller}/{one}/{two}/{three}/{four}/{five}/{productdetail} 
     {controller}/{one}/{two}/{three}/{four}/{productdetail} 
     {controller}/{one}/{two}/{three}/{productdetail} 
     {controller}/{one}/{two}/{productdetail}
    
    如果不创建路由约束以确保{one}不是{controller}的操作,则无法为{controller}/{one}/{productdetail}注册路由

    我强烈建议,如果你有选项1-5的列表,你可以创建一个自定义的路由约束来验证它们,这样你就不会意外地匹配一条你不想匹配的路由,但是你应该安全地使用我列出的上述路由

    创建IRouteConstraint并不困难。下面是我之前为路由约束编写的一些代码,该约束允许在不需要指定控制器的情况下从特定控制器调用操作。例如,一个名为Home的控制器带有一个“About”操作,该约束允许您调用/About而不是/Home/About

    它与您想要做的事情相关,因为它向您展示了如何进行验证,以区分{one}和{action}之间的差异

    路线约束:

    public class IsRootActionConstraint : IRouteConstraint
    {
        private List<string> _actions;
    
        public IsRootActionConstraint(): this( "homecontroller")
        {
    
        }
        public IsRootActionConstraint(string ControllerName)
        {
            Type _type = Assembly
                                .GetCallingAssembly()
                                .GetTypes()
                                .Where(type => type.IsSubclassOf(typeof(Controller)) && type.Name.ToLower() == ControllerName.ToLower())
                                .SingleOrDefault();
            if (_type != null)
            {
                _actions = (from methods in _type.GetMethods() where typeof(ActionResult).IsAssignableFrom(methods.ReturnType) select methods.Name.ToLower()).ToList();
            }
        }
    
        #region IRouteConstraint Members
    
        public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
        {
            return _actions.Contains((values["action"] as string).ToLower());
        }
    
        #endregion
    }
    

    在您的情况下,验证{one}与{controller}中的路由不匹配应该不会太困难。您可以将反射代码移到Match方法中,并使用控制器路由值中的名称来查找操作。

    解决此问题的最简单方法是为5、4、3和2个参数注册路由。按顺序登记

     {controller}/{one}/{two}/{three}/{four}/{five}/{productdetail} 
     {controller}/{one}/{two}/{three}/{four}/{productdetail} 
     {controller}/{one}/{two}/{three}/{productdetail} 
     {controller}/{one}/{two}/{productdetail}
    
    如果不创建路由约束以确保{one}不是{controller}的操作,则无法为{controller}/{one}/{productdetail}注册路由

    我强烈建议,如果你有选项1-5的列表,你可以创建一个自定义的路由约束来验证它们,这样你就不会意外地匹配一条你不想匹配的路由,但是你应该安全地使用我列出的上述路由

    创建IRouteConstraint并不困难。下面是我之前为路由约束编写的一些代码,该约束允许从特定控制器调用操作,而无需指定