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
Asp.net mvc 我可以使用MVC路由限制页码吗?_Asp.net Mvc_Pagination_Asp.net Mvc Routing - Fatal编程技术网

Asp.net mvc 我可以使用MVC路由限制页码吗?

Asp.net mvc 我可以使用MVC路由限制页码吗?,asp.net-mvc,pagination,asp.net-mvc-routing,Asp.net Mvc,Pagination,Asp.net Mvc Routing,我的URL如下所示: /category/page-# /tag/product/page-# ...... routes.MapRoute( name: "limitPaging", url: "*/page-{pageNumber}", defaults: new { controller = "Error", action = "P404", }, new { pageNumber = @"\d+" }, new { p

我的URL如下所示:

/category/page-#

/tag/product/page-#

......
routes.MapRoute(
  name: "limitPaging",
  url: "*/page-{pageNumber}",
  defaults: new { controller = "Error", action = "P404", }, 
            new { pageNumber = @"\d+" }, 
            new { pageNumber > 200 }
);
public class LessThanPage : IRouteConstraint
    {
        private int _maxPage;

        public LessThanPage(int maxPage)
        {
            _maxPage = maxPage;
        }

        public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
        {
            return httpContext.Request.RawUrl.ToLower().Contains("/page-") && GetPageNumberFromURL(httpContext.Request.RawUrl.ToLower()) > _maxPage;
        }

        private int GetPageNumberFromURL(string url)
        {
            var pageNumber = 1;
            var iIndexOfPage = url.IndexOf("/page-");
            var iIndexOfHash = url.IndexOf('#') > -1 ? url.IndexOf('#') : url.Length;
            if (iIndexOfPage >= 0 && iIndexOfHash - iIndexOfPage > 0)
                pageNumber = int.Parse(url.Substring(iIndexOfPage, iIndexOfHash - iIndexOfPage).Split('-')[1]);

            return pageNumber;
        }
    }
我可以使用MVC路由来限制页码吗? 我想做这样的事情:

/category/page-#

/tag/product/page-#

......
routes.MapRoute(
  name: "limitPaging",
  url: "*/page-{pageNumber}",
  defaults: new { controller = "Error", action = "P404", }, 
            new { pageNumber = @"\d+" }, 
            new { pageNumber > 200 }
);
public class LessThanPage : IRouteConstraint
    {
        private int _maxPage;

        public LessThanPage(int maxPage)
        {
            _maxPage = maxPage;
        }

        public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
        {
            return httpContext.Request.RawUrl.ToLower().Contains("/page-") && GetPageNumberFromURL(httpContext.Request.RawUrl.ToLower()) > _maxPage;
        }

        private int GetPageNumberFromURL(string url)
        {
            var pageNumber = 1;
            var iIndexOfPage = url.IndexOf("/page-");
            var iIndexOfHash = url.IndexOf('#') > -1 ? url.IndexOf('#') : url.Length;
            if (iIndexOfPage >= 0 && iIndexOfHash - iIndexOfPage > 0)
                pageNumber = int.Parse(url.Substring(iIndexOfPage, iIndexOfHash - iIndexOfPage).Split('-')[1]);

            return pageNumber;
        }
    }

谢谢

您可以创建一个自定义路由约束(一个实现
IRouteConstraint
的类)

公共类LessThanPage:IRouteConstraint
{
专用int_maxPage;
公共LessThanPage(int-maxPage)
{
_maxPage=maxPage;
}
公共布尔匹配(HttpContextBase httpContext、路由路由、字符串参数名称、RouteValueDictionary值、RouteDirection RouteDirection)
{
返回Convert.ToInt32(值[parameterName].ToString())new-LessThanPage(200)}
);

您还可以使用此方法验证参数是否为数字,从而在@Stephen Muecke answer的帮助下删除正则表达式约束

/category/page-#

/tag/product/page-#

......
routes.MapRoute(
  name: "limitPaging",
  url: "*/page-{pageNumber}",
  defaults: new { controller = "Error", action = "P404", }, 
            new { pageNumber = @"\d+" }, 
            new { pageNumber > 200 }
);
public class LessThanPage : IRouteConstraint
    {
        private int _maxPage;

        public LessThanPage(int maxPage)
        {
            _maxPage = maxPage;
        }

        public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
        {
            return httpContext.Request.RawUrl.ToLower().Contains("/page-") && GetPageNumberFromURL(httpContext.Request.RawUrl.ToLower()) > _maxPage;
        }

        private int GetPageNumberFromURL(string url)
        {
            var pageNumber = 1;
            var iIndexOfPage = url.IndexOf("/page-");
            var iIndexOfHash = url.IndexOf('#') > -1 ? url.IndexOf('#') : url.Length;
            if (iIndexOfPage >= 0 && iIndexOfHash - iIndexOfPage > 0)
                pageNumber = int.Parse(url.Substring(iIndexOfPage, iIndexOfHash - iIndexOfPage).Split('-')[1]);

            return pageNumber;
        }
    }
路由语法:

routes.MapRoute(
              name: "limitPaging",
              url: "{*url}",
              defaults: new { controller = "Error", action = "P404" },
              constraints: new { pageNumber = new LessThanPage(200), }
            );