예제 #1
0
        public string GetPagerHtml()
        {
            StringBuilder html = new StringBuilder();

            html.Append("<ul>");

            //ToDO:加上上一页、下一页、首页、末页、页面跳转等。

            //总页数
            int pageCount = (int)Math.Ceiling(TotalCount * 1.0 / PageSize);
            //显示出来的页码的起始页码
            int startPageIndex = Math.Max(1, PageIndex - MaxPagerCount / 2);
            //显示出来的页码的结束页码
            int endPageIndex = Math.Min(pageCount, startPageIndex + MaxPagerCount);

            for (int i = startPageIndex; i <= endPageIndex; i++)
            {
                //是当前页
                if (i == PageIndex)
                {
                    html.Append("<li class='").Append(CurrentPageClassName).Append("'>")
                    .Append(i).Append("</li>");
                }
                else
                {
                    string href = UrlPattern.Replace("{pn}", i.ToString());
                    html.Append("<li><a href='").Append(href).Append("'>")
                    .Append(i).Append("</a></li>");
                }
            }

            html.Append("</ul>");
            return(html.ToString());
        }
예제 #2
0
        public static bool Match(this UrlPattern urlPattern, string url)
        {
            switch (urlPattern.Type)
            {
            case UrlPatternType.StartWith:
                return(url.StartsWith(urlPattern.Pattern));

            case UrlPatternType.Contains:
                return(url.Contains(urlPattern.Pattern));

            case UrlPatternType.EndWith:
                return(url.EndsWith(urlPattern.Pattern));

            case UrlPatternType.Regex:
                try
                {
                    return(urlPattern.Regex.Match(url).Success);
                }
                catch (Exception ex)
                {
                    LogTo.Error(ex, $"Exception while matching regex url pattern '{urlPattern.Pattern}' with '{url}'.");
                    throw;
                }

            default:
                throw new NotImplementedException($"Feed filter type {urlPattern.Type} not implemented");
            }
        }
예제 #3
0
        public string GetPagerHtml()
        {
            StringBuilder html = new StringBuilder();

            html.Append("<ul>");
            //总页数
            int pageCount = (int)Math.Ceiling(TotalCount * 1.0f / PageSize);
            //FirstPage = 1;
            //LastPage = pageCount;
            //NextPage =(PageIndex + 1).ToString();
            //FrontPage = pageCount - 1;
            //显示出来的页码的起始页码
            int startPageIndex = (int)Math.Max(1, PageIndex - MaxPagerCount / 2);     //显示第一个页码
            int endPageIndex   = Math.Min(pageCount, startPageIndex + MaxPagerCount); //显示出来的最后一个页码

            for (int i = startPageIndex; i <= endPageIndex; i++)
            {
                //是当前页码
                if (i == PageIndex)
                {
                    html.Append("<li class='").Append(CurrentPageClassName).Append(i).Append(">").Append("</li>");
                    //html.Append("<li> class=''").Append("</li>")
                }
                else
                {
                    string href = UrlPattern.Replace("{pn}", i.ToString());
                    html.Append("<li><a href='").Append(href).Append("'>").Append(i).Append("</a></li>");
                }
            }
            html.Append("</ul>");
            return(html.ToString());
        }
예제 #4
0
        public string GetPagerHtml()
        {
            StringBuilder html = new StringBuilder();

            html.Append("<ul>");
            int pageCount = (int)Math.Ceiling(TotalCount * 1.0f / PageSize);

            int startPageIndex = Math.Max(1, PageIndex - MaxPagerCount / 2);
            int endPageIndex   = Math.Min(pageCount, startPageIndex + MaxPagerCount - 1);

            for (int i = startPageIndex; i <= endPageIndex; i++)
            {
                if (i == PageIndex)
                {
                    html.AppendLine("<li class='").Append(CurrentPageClassName).Append("'>").Append(i).Append("</li>");
                }
                else
                {
                    string href = UrlPattern.Replace("{pn}", i.ToString());
                    html.AppendLine("<li><a href='href'>").Append(i).Append("</a></li>");
                }
            }
            html.Append("</ul>");
            return(html.ToString());
        }
예제 #5
0
        public string GetPagerHtml()
        {
            StringBuilder html = new StringBuilder();

            //首页、末页、上一页、下一页、跳转到N页

            //总页数=总数据条数/每页数据条数
            int pageCount = (int)Math.Ceiling(TotalCount * 1.0 / PageSize);
            //显示出来的页码的其实页码
            int startPageIndex = Math.Max(1, PageIndex - MaxPagerCount / 2);
            //显示出来的页码的结束页码
            int endPageIndex = Math.Min(pageCount, startPageIndex + MaxPagerCount - 1);

            html.Append("<ul>");
            for (int i = startPageIndex; i <= endPageIndex; i++)
            {
                //判断是不是当前页
                if (i == PageIndex)
                {
                    html.Append("<li class='").Append(CurrentPageClassName).Append("'>").Append(i).Append("</li>");
                }
                else
                {
                    html.Append("<li><a href='").Append(UrlPattern.Replace("{pn}", i.ToString()))
                    .Append("'>").Append(i).Append("</a>")
                    .Append("</li>");
                }
            }

            html.Append("</ul>");
            return(html.ToString());
        }
        public static bool ValidateFilter(this UrlPattern urlPattern, out string error)
        {
            error = null;

            switch (urlPattern.Type)
            {
            case UrlPatternType.Regex:
                try
                {
                    // ReSharper disable once ObjectCreationAsStatement
                    new Regex(urlPattern.Pattern);

                    return(true);
                }
                catch (Exception ex)
                {
                    error = ex.Message;
                }

                return(false);

            default:
                return(true);
            }
        }
예제 #7
0
		public void Should_throw_an_exception()
		{
			const string pattern = "{controller}/{controller";

			var url = new UrlPattern(pattern);

			Assert.Throws<RoutingException>(() => url.ToRouteDefinition());
		}
예제 #8
0
		public IEnumerable<RouteParameter> Parse(UrlPattern pattern)
		{
			IList<RouteParameter> parameters = new List<RouteParameter>();

			ParsePattern(pattern.ToString(), parameterName => { parameters.Add(new RouteParameterImpl(parameterName)); });

			return parameters;
		}
예제 #9
0
        public IEnumerable <RouteParameter> Parse(UrlPattern pattern)
        {
            IList <RouteParameter> parameters = new List <RouteParameter>();

            ParsePattern(pattern.ToString(), parameterName => { parameters.Add(new RouteParameterImpl(parameterName)); });

            return(parameters);
        }
예제 #10
0
        public void Should_throw_an_exception()
        {
            const string pattern = "{controller/controller}";

            var url = new UrlPattern(pattern);

            Assert.Throws <RoutingException>(() => url.ToRouteDefinition());
        }
예제 #11
0
        public string GetPagerHtml()
        {
            StringBuilder sb = new StringBuilder();

            //算出来的页数
            PageCount = (int)Math.Ceiling(TotalCount * 1.0f / PageSize);
            int startPageIndex = Math.Max(1, PageIndex - MaxPagerCount / 2);              //第一个页码
            int endPageIndex   = Math.Min(PageCount, startPageIndex + MaxPagerCount - 1); //最后一个页码

            sb.AppendLine("<div style='margin-top: 50px;'>");
            sb.AppendLine("<ul id='page' class='pagination'>");
            if (PageIndex > 1)
            {
                sb.AppendLine("<li><a href='javascript:getPage(1);' data-original-title='' title=''>首页</a></li>");
                sb.Append("<li><a href='").Append(UrlPattern.Replace("{pn}", (PageIndex - 1).ToString())).Append("' data -original-title='' title=''>上一页</a></li>").AppendLine();
            }
            else
            {
                sb.AppendLine("<li><a data-original-title='' title=''>首页</a></li>");
                sb.AppendLine("<li><a data-original-title='' title=''>上一页</a></li>");
            }
            List <Page> lists = new List <Page>();

            for (int i = startPageIndex; i <= endPageIndex; i++)
            {
                Page page = new Page();
                if (i == PageIndex)
                {
                    page.PageIndex = i;
                    page.Current   = "active";
                    sb.Append("<li class='").Append(CurrentLinkClassName).Append(" active'><a data-original-title='' title=''>").Append(i).Append("</a></li>").AppendLine();
                }
                else
                {
                    page.PageIndex = i;
                    page.Current   = "";
                    sb.Append("<li><a data-original-title='' title='' href='").Append(UrlPattern.Replace("{pn}", i.ToString())).Append("'>").Append(i).Append("</a></li>").AppendLine();
                }
                lists.Add(page);
            }
            if (PageIndex < PageCount)
            {
                sb.Append("<li><a href='").Append(UrlPattern.Replace("{pn}", (PageCount).ToString())).Append("' data -original-title='' title=''>尾页</a></li>");
                sb.Append("<li><a href='").Append(UrlPattern.Replace("{pn}", (PageIndex + 1).ToString())).Append("' data -original-title='' title=''>下一页</a></li>").AppendLine();
            }
            else
            {
                sb.AppendLine("<li><a data-original-title='' title=''>下一页</a></li>");
                sb.AppendLine("<li><a data-original-title='' title=''>尾页</a></li>");
            }
            sb.AppendLine("<li><span><input type='text'id='txtPage' style='width: 30px;height: 20px;'></span></li><li><span><a id='go' data-original-title='' title=''>跳转</a></span></li>");
            sb.AppendLine("</ul>");
            sb.AppendLine("</div>");
            Pages = lists;
            return(sb.ToString());
        }
예제 #12
0
        public void MatchWithPath()
        {
            var up = new UrlPattern(null, new Regex(@"^/path/file\.txt$"), null);

            Assert.IsTrue(up.IsMatch(new Uri("http://localhost/path/file.txt")));
            Assert.IsTrue(up.IsMatch(new Uri("http://localhost/path/file.txt?query=1")));

            Assert.IsFalse(up.IsMatch(new Uri("http://host/xyz/path/file.txt")));
            Assert.IsFalse(up.IsMatch(new Uri("http://host/path/file.txt/abc")));
            Assert.IsFalse(up.IsMatch(new Uri("http://host/path/file.txt/")));
        }
예제 #13
0
        public bool IsMatch(string url)
        {
            if (String.IsNullOrEmpty(UrlPattern))
            {
                return(true);
            }

            var pattern = Regex.Replace(UrlPattern.ToLowerInvariant(), @"{(.*?)}", @"(.*?)");
            var match   = Regex.Match(url.ToLowerInvariant(), pattern);

            return(match.Success);
        }
예제 #14
0
        public virtual Route New(UrlPattern pattern)
        {
            try
            {
                IEnumerable <RouteParameter> parameters = _parser.Parse(pattern);

                var routeDefinition = new RouteImpl(pattern, parameters, Enumerable.Empty <RouteVariable>());

                return(routeDefinition);
            }
            catch (Exception ex)
            {
                throw new RoutingException("The route could not be created", ex);
            }
        }
예제 #15
0
		public virtual Route New(UrlPattern pattern)
		{
			try
			{
				IEnumerable<RouteParameter> parameters = _parser.Parse(pattern);

				var routeDefinition = new RouteImpl(pattern, parameters, Enumerable.Empty<RouteVariable>());

				return routeDefinition;
			}
			catch (Exception ex)
			{
				throw new RoutingException("The route could not be created", ex);
			}
		}
예제 #16
0
        public void MatchWithHost()
        {
            var up = new UrlPattern(new Regex("^host$"), null, null);

            Assert.IsTrue(up.IsMatch(new Uri("http://host")));
            Assert.IsTrue(up.IsMatch(new Uri("http://host/")));
            Assert.IsTrue(up.IsMatch(new Uri("http://host:123")));
            Assert.IsTrue(up.IsMatch(new Uri("http://host/path")));
            Assert.IsTrue(up.IsMatch(new Uri("http://host/?query=1")));

            Assert.IsFalse(up.IsMatch(new Uri("http://unknown")));
            Assert.IsFalse(up.IsMatch(new Uri("http://host1")));
            Assert.IsFalse(up.IsMatch(new Uri("http://nohost")));
            Assert.IsFalse(up.IsMatch(new Uri("http://unknown/")));
            Assert.IsFalse(up.IsMatch(new Uri("http://unknown/path")));
        }
예제 #17
0
        public void MatchWithQuery()
        {
            var up1 = new UrlPattern(null, null, new Dictionary<string, Regex>
            {
                 {"abc", null }
            });

            Assert.IsTrue(up1.IsMatch(new Uri("http://host/path?abc")));
            Assert.IsTrue(up1.IsMatch(new Uri("http://host/path?abc=")));
            Assert.IsTrue(up1.IsMatch(new Uri("http://host/path?abc=456")));
            Assert.IsTrue(up1.IsMatch(new Uri("http://host/path?xyz=123&abc")));
            Assert.IsTrue(up1.IsMatch(new Uri("http://host/path?xyz=123&abc=")));
            Assert.IsTrue(up1.IsMatch(new Uri("http://host/path?xyz=123&abc=456")));

            Assert.IsFalse(up1.IsMatch(new Uri("http://host/path")));
            Assert.IsFalse(up1.IsMatch(new Uri("http://host/path?abcd")));
            Assert.IsFalse(up1.IsMatch(new Uri("http://host/path?xyz=123")));
            Assert.IsFalse(up1.IsMatch(new Uri("http://host/path?xyz=123&abcd")));
        }
예제 #18
0
        public string GetPagerHtml()
        {
            StringBuilder html = new StringBuilder();

            html.Append("<div  class='layui-box layui-laypage layui-laypage-default'>");
            //ToDO:加上上一页、下一页、首页、末页、页面跳转等。
            //总页数
            int pageCount = (int)Math.Ceiling(TotalCount * 1.0 / PageSize);
            //显示出来的页码的起始页码
            int startPageIndex = Math.Max(1, PageIndex - MaxPagerCount / 2);
            //显示出来的页码的结束页码
            int    endPageIndex = Math.Min(pageCount, startPageIndex + MaxPagerCount);
            string href         = "";

            //上一页
            href = UrlPattern.Replace("{pn}", (PageIndex - 1) == 0 ? "1" : (PageIndex - 1).ToString());
            html.Append("<a href='").Append(href).Append("' class='layui-laypage-prev'>")
            .Append("上一页").Append("</a>");
            for (int i = startPageIndex; i <= endPageIndex; i++)
            {
                //是当前页
                if (i == PageIndex)
                {
                    html.Append("<span class='layui-laypage-curr'><em class='layui-laypage-em'></em><em>")
                    .Append(i).Append("</em></span>");
                }
                else
                {
                    href = UrlPattern.Replace("{pn}", i.ToString());
                    html.Append("<a href='").Append(href).Append("'>")
                    .Append(i).Append("</a>");
                }
            }
            //下一页
            href = UrlPattern.Replace("{pn}", (PageIndex + 1) >= endPageIndex ? endPageIndex.ToString() : (PageIndex + 1).ToString());
            html.Append("<a href='").Append(href).Append("' class='layui-laypage-next'>")
            .Append("下一页").Append("</a>");


            html.Append("</div>");
            return(html.ToString());
        }
예제 #19
0
        public string GetPager()
        {
            StringBuilder sb = new StringBuilder();
            //算出来的页数
            int pageCount      = (int)Math.Ceiling(TotalCount * 1.0f / PageSize);
            int startPageIndex = Math.Max(1, PageIndex - MaxPagerCount / 2);              //第一个页码
            int endPageIndex   = Math.Min(pageCount, startPageIndex + MaxPagerCount - 1); //最后一个页码

            sb.AppendLine("<ul>");
            if (startPageIndex == PageIndex)
            {
                sb.AppendLine("<li>上一页</li>");
            }
            else
            {
                sb.AppendLine("<li><a href='" + UrlPattern.Replace("{pn}", (PageIndex - 1).ToString()) + "'>上一页</a></li>");
            }

            for (int i = startPageIndex; i <= endPageIndex; i++)
            {
                if (i == PageIndex)
                {
                    sb.AppendLine("<li class='" + CurrentLinkClassName + "'>" + i + "</li>");
                }
                else
                {
                    sb.AppendLine("<li><a href='" + UrlPattern.Replace("{pn}", i.ToString()) + "'>" + i +
                                  "</a></li>");
                }
            }
            if (endPageIndex == PageIndex)
            {
                sb.AppendLine("<li>下一页</li>");
            }
            else
            {
                sb.AppendLine("<li><a href='" + UrlPattern.Replace("{pn}", (PageIndex + 1).ToString()) + "'>下一页</a></li>");
            }
            sb.AppendLine("</ul>");
            return(sb.ToString());
        }
예제 #20
0
        public List <UrlSegment> CreateSegments()
        {
            var segments = new List <UrlSegment>();

            foreach (var segment in UrlPattern.Split('/'))
            {
                if (segment.StartsWith("{") && segment.EndsWith("}"))
                {
                    segments.Add(new UrlSegment {
                        Segment = segment.Replace("{", "").Replace("}", ""), IsVariable = true
                    });
                }
                else
                {
                    segments.Add(new UrlSegment {
                        Segment = segment
                    });
                }
            }
            return(segments);
        }
예제 #21
0
        public string GetPagerHtml()
        {
            StringBuilder html = new StringBuilder();

            html.Append("<ul>");

            //总页数=总条数/页每页的条数(取天花板数Ceiling)
            int pageCount = (int)Math.Ceiling(TotalCount * 1.0 / PageSize);

            //显示出来的页码的起始页码( 1和当前页码减去显示出来最多页码数除以2对比谁大显示谁)
            //当前页是20页,总共显示出来10页,20-10/2=15 ,15比1大,起始页显示15
            int startPageIndex = Math.Max(1, PageIndex - MaxPagerCount / 2);

            //显示出来的页码的结束页码
            //起始页+显示出来最多的页码数 和 总页数比较谁小取谁
            //总页数15  起始页4+最多显示出来10条,那最后的页码是14
            int endPageIndex = Math.Min(pageCount, startPageIndex + MaxPagerCount);

            //起始页是1,最大也是10 当前页是5的话,5拼接样式属性,没有超链接
            for (int i = startPageIndex; i <= endPageIndex; i++)
            {
                //如果是当前页
                if (i == PageIndex)
                {
                    //设置当前页的样式
                    html.Append("<li class='").Append(CurrentPageClassName).Append("'>")
                    .Append(i).Append("</li>");
                }
                else
                {
                    string href = UrlPattern.Replace("{pn}", i.ToString());
                    html.Append("<li><a href='").Append(href).Append("'>")
                    .Append(i).Append("</a></li>");
                }
            }
            html.Append("</ul>");
            return(html.ToString());
        }
예제 #22
0
        public new IHttpHandler GetHttpHandler(RequestContext requestContext, UrlPattern pattern)
        {
            if(String.Equals("dashboard",pattern.SecondLevel,StringComparison.CurrentCultureIgnoreCase))
            {
                Controller = "Administration";
                Action = "Dashboard";
            }
            else if (String.Equals("Menu", pattern.SecondLevel, StringComparison.CurrentCultureIgnoreCase))
            {
                Controller = "Menu";
                Action = "MenuMaster";
            }
            else if (String.Equals("CreateMenu", pattern.SecondLevel, StringComparison.CurrentCultureIgnoreCase))
            {
                Controller = "Menu";
                Action = "CreateMenu";
            }

            requestContext.RouteData.Values["controller"] = Controller;
            requestContext.RouteData.Values["action"] = Action;

            return base.GetHttpHandler(requestContext);
        }
예제 #23
0
        public string GetPagerHtml()
        {
            StringBuilder sb = new StringBuilder();
            //算出来的页数
            int pageCount      = (int)Math.Ceiling(TotalCount * 1.0f / PageSize);
            int startPageIndex = Math.Max(1, PageIndex - MaxPagerCount / 2);              //第一个页码
            int endPageIndex   = Math.Min(pageCount, startPageIndex + MaxPagerCount - 1); //最后一个页码

            sb.AppendLine("<ul><li>第</li>");
            for (int i = startPageIndex; i <= endPageIndex; i++)
            {
                if (i == PageIndex)
                {
                    sb.Append("<li class='").Append(CurrentLinkClassName).Append("'>").Append(i).Append("</li>").AppendLine();
                }
                else
                {
                    sb.Append("<li><a href='").Append(UrlPattern.Replace("{pn}", i.ToString())).Append("'>").Append(i).Append("</a></li>").AppendLine();
                }
            }
            sb.AppendLine("<li>页</li></ul>");
            sb.Append("<input type='text' id='setIndex' style='width: 45px; height: 20px'>&nbsp;&nbsp;&nbsp;&nbsp;<input type='button' id='getPage' value='跳转' style='height: 23px'>");
            return(sb.ToString());
        }
예제 #24
0
		public void Setup()
		{
			_url = new UrlPattern(Pattern);
			_route = _url.ToRouteDefinition();
		}
예제 #25
0
        public void MatchWithoutPatterns()
        {
            var up = new UrlPattern(null, null, null);

            Assert.IsTrue(up.IsMatch(new Uri("http://host/path?query=1")));
        }
예제 #26
0
 /// <summary>
 /// Replace variables in UrlPattern to create a url to a page.
 /// In the future other variables can be added such as sort and filter
 ///
 /// Variables are:
 ///
 /// {page} - will get replaced with page number
 /// </summary>
 /// <param name="page"></param>
 /// <returns></returns>
 private string GetUrl(int page)
 {
     return(UrlPattern.Replace("{page}", page.ToString()));
 }
예제 #27
0
 public static ActionResult RedirectTo(this ControllerBase self, UrlPattern zeroArgumentPath, bool permanent = false)
 {
     return(self.RedirectTo(zeroArgumentPath.Url, permanent));
 }
예제 #28
0
 public void Setup()
 {
     _url   = new UrlPattern(Pattern);
     _route = _url.ToRouteDefinition();
 }
예제 #29
0
 public static Route ToRouteDefinition(this UrlPattern pattern)
 {
     return(RouteFactory.Current.New(pattern));
 }
예제 #30
0
        private void ParseUrlPattern()
        {
            if (String.IsNullOrEmpty(UrlPattern) || String.IsNullOrWhiteSpace(UrlPattern))
            {
                SetInvalid("Url pattern is null, empty, or nothing but whitespace");

                return;
            }

            if (UrlPattern[0] != '/')
            {
                SetInvalid("Url pattern must open with '/'");

                return;
            }

            string[] matches = UrlPattern.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);

            if (matches == null || matches.Length == 0)
            {
                SillySegment rootSegment = new SillyHardCodedSegment();

                Add(rootSegment);

                return;
            }

            bool controllerSet = false, methodSet = false;

            foreach (string match in matches)
            {
                if (String.Compare("/", match, true) == 0)
                {
                    continue;
                }

                SillySegment segment = ProduceFromSegment(match);

                if (segment == null)
                {
                    SetInvalid("Cannot find a suitable match for Url segment '" + match + "'");

                    return;
                }

                if (segment.Type == SegmentTypes.Controller)
                {
                    if (controllerSet)
                    {
                        SetInvalid("Cannot define multiple :controller segments in a Url pattern");

                        return;
                    }

                    controllerSet = true;

                    if (methodSet)
                    {
                        SetInvalid("Cannot define :method before :controller in a Url pattern");

                        return;
                    }

                    if (VarCount > 0)
                    {
                        SetInvalid("Cannot define {vars} before :controller in a Url pattern");

                        return;
                    }
                }

                if (segment.Type == SegmentTypes.Method)
                {
                    if (methodSet)
                    {
                        SetInvalid("Cannot define multiple :method segments in a Url pattern");

                        return;
                    }

                    methodSet = true;

                    if (VarCount > 0)
                    {
                        SetInvalid("Cannot define {vars} before :method in a Url pattern");

                        return;
                    }
                }

                Add(segment);
            }
        }