使用自定义标记帮助程序MVC CORE路由系统

时间:2018-06-15 02:03:11

标签: c# asp.net-mvc asp.net-core asp.net-core-mvc

自定义标记助手'分页'

[HtmlTargetElement("div", Attributes = "page-model")]
    public class PageLinkTagHelper : TagHelper
    {
        private IUrlHelperFactory urlHelperFactory;
        public PageLinkTagHelper(IUrlHelperFactory helperFactory)
        {
            urlHelperFactory = helperFactory;
        }

        [ViewContext]
        [HtmlAttributeNotBound]
        public ViewContext ViewContext { get; set; }
        public PagingInfo PageModel { get; set; }
        public string PageRoute { get; set; }
        [HtmlAttributeName(DictionaryAttributePrefix = "page-url-")]
        public Dictionary<string, object> PageUrlValues { get; set; } = new Dictionary<string, object>();
        public bool PageClassesEnabled { get; set; } = false;
        public string PageClass { get; set; }
        public string PageClassNormal { get; set; }
        public string PageClassSelected { get; set; }

        public override void Process(TagHelperContext context, TagHelperOutput output)
        {
            IUrlHelper urlHelper = urlHelperFactory.GetUrlHelper(ViewContext);
            TagBuilder result = new TagBuilder("div");
            for (int i = 1; i <= PageModel.TotalPages; i++)
            {
                TagBuilder tag = new TagBuilder("a");
                tag.Attributes["href"] = urlHelper.RouteUrl(PageRoute, new { page = i });
                PageUrlValues["page"] = i;
                tag.Attributes["href"] = urlHelper.RouteUrl(PageRoute, PageUrlValues);
                if (PageClassesEnabled)
                {
                    tag.AddCssClass(PageClass);
                    tag.AddCssClass(i == PageModel.CurrentPage
                    ? PageClassSelected : PageClassNormal);
                }
                tag.InnerHtml.Append(i.ToString());
                result.InnerHtml.AppendHtml(tag);
            }
            output.Content.AppendHtml(result.InnerHtml);
        }
    }

查看

<div page-model="@Model.PagingInfo" page-route="@(Model.IsSysParamCategory ? RouteUrl.Name.SystemListPagination : RouteUrl.Name.CategoryListPagination)" page-classes-enabled="true" page-class="btn mt-2 mb-2" page-class-normal="btn-secondary" page-class-selected="btn-outline-primary" class="btn-group pull-right m-1"></div>

控制器 - 路线

[Route("Admin/System/[action]/{page:int}", Name = RouteUrl.Name.SystemListPagination)]
[Route("Admin/System/[action]/{page:int}", Name = RouteUrl.Name.CategoryListPagination)]
public ViewResult List(int valueId, bool isSysParamCategory, DeleteCategoryType deletedCategory, SaveCategoryType savedCategory, int page = 1)
{ }

html的生成方式如下: enter image description here

错误: InvalidOperationException:找不到视图“列表”。搜索了以下位置:  /Areas/Admin/Views/Shared/List.cshtml  /Views/Shared/List.cshtml

问题和反思

我只是不明白:)我正在使用这个路径名称:'Admin/System/List/{page:int}''Admin/Category/List/{page:int}',具体取决于逻辑标志,但MVC框架是'Shared'文件夹中的搜索视图。查看位于:Areas/Admin/Views/CategoryType/List

1 个答案:

答案 0 :(得分:2)

我昨天花了一些时间在这个错误上,现在在5分钟内找到解决方案!

solution

我收到此错误,因为单词Page在Asp.net Core 2中保留