Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.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
C# 在RouteConstraint中操作RouteValueDictionary_C#_.net_Model View Controller_Routing_Route Constraint - Fatal编程技术网

C# 在RouteConstraint中操作RouteValueDictionary

C# 在RouteConstraint中操作RouteValueDictionary,c#,.net,model-view-controller,routing,route-constraint,C#,.net,Model View Controller,Routing,Route Constraint,假设我有一条路线; www.kunduz.com/stuff/something 根据路线约束检查“某物” public class AnalysisTypePathRouteConstraint : IRouteConstraint { public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection

假设我有一条路线; www.kunduz.com/stuff/something

根据路线约束检查“某物”

 public class AnalysisTypePathRouteConstraint : IRouteConstraint
{
    public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
    {
       //check if "something" is really something
    }
}
假设东西有这样的结构

public class Stuff
{
     public int Id{get;set;}
     public string name {get;set;}
     //some other properties
}
考虑到对象内容不仅仅是DB中的一个条目,更像是一种类型。比如,如果这是一个电子商务网站,它可以是“汽车”或“家具”

所以我要做的是,我要检查“某物”在我的路径约束上是否真的是有效的“东西”。然后在我的控制器中

Public class SomeController
{
    public GetStuff(string stuffName)
    {
        //get stuff by its name and get its Id
        //use that Id to do something else
    }
}
现在,我能做的就是

 public class AnalysisTypePathRouteConstraint : IRouteConstraint
{
    public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
    {
       //check if "something" is really something
       values.Add("stuffId",stuff.Id");
    }
}
并获取我添加为控制器操作参数的Id

    public GetStuff(int stuffId)
    {
        //get stuff by its name and get its Id
        //use that Id to do something else
    }
这可能会提高性能,对我来说,避免两次获取东西Id更有意义

我的问题是,这是一种好的做法吗?由于我的URL不包含sutffId,我的控制器操作可能会让未来的开发人员在查看此代码时感到困惑

我真的很想了解一下这件事


谢谢。

不要依赖某些
RouteConstraint
来修改值。这不是它的本意。但是,为什么您不能在您的操作中检查
stuffName
参数并采取相应的行动呢?因为如果“stuff”不存在,客户不希望该路线存在。将其视为电子商务网站中的一个类别。如果他们不出售家具,那么www.buystuff/furnitare url就不应该存在。用HTTP术语来说,对于这些情况,适当的响应就是
404notfound
。验证控制器/操作中的输入并
返回HttpNotFound()
以查找无效值没有问题。这正是当您的
RouteConstraint.Match()
返回
false
并且没有其他路由与Url匹配时所得到的结果。对于这些情况,某些控制器中的操作必须获取
stringparameter
并确定
stringparameter
代表什么(类别或产品)然后
退货产品(参数)
/
退货类别(参数)
。然而,将
/category/{stringparameter}
匹配到
category
/product/{stringparameter}
匹配到
product
的简单路由可能更干净。对我来说,
RouteConstraint
仅当它可以基于
HttpContext
返回
true/false
时才适用。例如,从查询字符串中提取值,检查某些自定义标题等。任何涉及实际业务逻辑的内容都不属于那里(并确定
someString
是否表示类别或产品对我来说听起来像是业务逻辑)。不要依赖某些
RouteConstraint
来修改值。这不是它的本意。但是,为什么您不能在您的操作中检查
stuffName
参数并采取相应的行动呢?因为如果“stuff”不存在,客户不希望该路线存在。将其视为电子商务网站中的一个类别。如果他们不出售家具,那么www.buystuff/furnitare url就不应该存在。用HTTP术语来说,对于这些情况,适当的响应就是
404notfound
。验证控制器/操作中的输入并
返回HttpNotFound()
以查找无效值没有问题。这正是当您的
RouteConstraint.Match()
返回
false
并且没有其他路由与Url匹配时所得到的结果。对于这些情况,某些控制器中的操作必须获取
stringparameter
并确定
stringparameter
代表什么(类别或产品)然后
退货产品(参数)
/
退货类别(参数)
。然而,将
/category/{stringparameter}
匹配到
category
/product/{stringparameter}
匹配到
product
的简单路由可能更干净。对我来说,
RouteConstraint
仅当它可以基于
HttpContext
返回
true/false
时才适用。例如,从querystring中提取值,检查某些自定义标题等。任何涉及实际业务逻辑的内容都不属于那里(并确定
someString
是否表示类别或产品对我来说听起来像业务逻辑)。