Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.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# 如何处理这个路由?_C#_Asp.net Mvc_Url_Routing_Routes - Fatal编程技术网

C# 如何处理这个路由?

C# 如何处理这个路由?,c#,asp.net-mvc,url,routing,routes,C#,Asp.net Mvc,Url,Routing,Routes,我有如下URL: /nl/blog(显示博客项目概述) /nl/blog/loont lekker koken en wordt eerlijkheid beloond(显示带有URL标题的博客项目) /nl/blog/waarom liever diëtist dan kok(显示带有URL标题的博客项目) 我已经为其定义了路线: A:使用约束articlepage=@“\d”路由“nl/blog/{articlepage}” B:路线“nl/博客” C:路由“nl/blog/{urlt

我有如下URL:

  • /nl/blog(显示博客项目概述)
  • /nl/blog/loont lekker koken en wordt eerlijkheid beloond(显示带有URL标题的博客项目)
  • /nl/blog/waarom liever diëtist dan kok(显示带有URL标题的博客项目)
我已经为其定义了路线:

  • A:使用约束articlepage=@“\d”路由“nl/blog/{articlepage}”
  • B:路线“nl/博客”
  • C:路由“nl/blog/{urlttitle}/{commentpage}”,带有约束commentpage=@“\d”
  • D:路由“nl/blog/{urlttitle}”
问题1:这很好,但也许有一个更好的解决方案,路线更少

问题2:要添加一篇新文章,我在BlogController中有一个操作方法AddArticle。当然,对于上面定义的路由,url“/nl/blog/addarticle”将映射到路由D,其中addarticle将是url标题,这当然是不正确的。因此,我添加了以下路线:

  • E:路由“nl/blog/{action}”
因此,现在url“/nl/blog/_addarticle”映射到此路由,并执行正确的操作方法。但我想知道是否有更好的方法来处理这个问题


谢谢你的建议。

回答我自己的问题:

对于问题1,我创建了一个自定义约束IsOptionalOrMatchesRegEx:

public class IsOptionalOrMatchesRegEx : IRouteConstraint
{
    private readonly string _regEx;

    public IsOptionalOrMatchesRegEx(string regEx)
    {
        _regEx = regEx;
    }

    public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
    {
        var valueToCompare = values[parameterName].ToString();
        if (string.IsNullOrEmpty(valueToCompare)) return true;
        return Regex.IsMatch(valueToCompare, _regEx);
    }
}
然后,路线A和B可以表示为一条路线:

  • url:“nl/blog/{articlepage}”
  • defaultvalues:new{articlepage=UrlParameter.Optional}
  • 约束条件:新建{articlepage=new isonationalOrmatchesRegex(@“\d”)
对于问题2,我创建了一个ExcludeConstraint:

public class ExcludeConstraint : IRouteConstraint
{
    private readonly List<string> _excludedList;

    public ExcludeConstraint(List<string> excludedList)
    {
        _excludedList = excludedList;
    }

    public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
    {
        var valueToCompare = (string)values[parameterName];
        return !_excludedList.Contains(valueToCompare);            
    }
}
公共类ExcludeConstraint:IRouteConstraint
{
私有只读列表_排除列表;
公共ExcludeConstraint(列表excludedList)
{
_excludedList=excludedList;
}
公共布尔匹配(HttpContextBase httpContext、路由路由、字符串参数名称、RouteValueDictionary值、RouteDirection RouteDirection)
{
var valueToCompare=(字符串)值[parameterName];
return!\u excludedList.Contains(valueToCompare);
}
}
路线D可以改变如下:

  • url:“nl/blog/{urlttitle}”
  • 约束:新的{urlttitle=new ExcludeConstraint(新列表(){“addarticle”、“addcomment”、“gettags”}))

问题很好。我对任何答案都很感兴趣。也许自定义路由约束可能是最优雅的解决方案?好的,对于问题2,我自己找到了答案。我创建了一个名为ExcludeConstraint的自定义约束,并将路由D更改为:路由“nl/blog/{urltitle}”,带有约束“new{urltitle=new ExcludeConstraint(new List(){”现在,我的url保持干净,像/nl/blog/addcomments。你可以对这些路由做的唯一更改,你的要求是将路由C和D结合起来,并添加一个
UrlParameter。可选的
commentPage声明。谢谢辅导员,这确实是答案的一部分!