public static async Task <IHtmlContent> GetMenu(this Microsoft.AspNetCore.Mvc.Razor.RazorPage razorPage, Delegate menuItemFunction) { if (menuItemFunction == null) { return(null); } IServiceProvider serviceProvider = razorPage.Context.RequestServices; object[] arguments = menuItemFunction.GetMethodInfo().GetParameters().Select(x => serviceProvider.GetService(x.ParameterType)).ToArray(); object funcReturnValue = menuItemFunction.DynamicInvoke(arguments); IEnumerable <IMenuItem> menuItems; if (funcReturnValue is IEnumerable <IMenuItem> ) { menuItems = (IEnumerable <IMenuItem>)funcReturnValue; } else if (funcReturnValue is Task <IEnumerable <IMenuItem> > ) { menuItems = await(Task <IEnumerable <IMenuItem> >) funcReturnValue; } else { menuItems = null; } if (menuItems == null) { return(null); } return(GetMenu(razorPage, menuItems)); }
/// <summary> /// /// </summary> /// <returns></returns> public static HtmlString Content(this Microsoft.AspNetCore.Mvc.Razor.RazorPage page, string path, params object[] args) { string contentPath = string.Format("~/" + path.Trim(new char[] { '/', '~', '\\', '.' }), args); return(new HtmlString(new UrlHelper(page.ViewContext).Content(contentPath))); }
public static IHtmlContent GetMenu(this Microsoft.AspNetCore.Mvc.Razor.RazorPage razorPage, IEnumerable <IMenuItem> menuItems) { if (menuItems == null) { return(null); } IUrlHelper urlHelper = razorPage.GetType().GetTypeInfo().GetProperty("Url").GetValue(razorPage) as IUrlHelper; if (urlHelper == null) { return(null); } HtmlContentBuilder builder = new HtmlContentBuilder(); foreach (IMenuItem menuItem in menuItems.Where(x => x != null)) { builder.AppendHtml(WriteMenuItemToBuilder(razorPage, urlHelper, menuItem)); } return(builder); }
static IHtmlContent WriteMenuItemToBuilder(this Microsoft.AspNetCore.Mvc.Razor.RazorPage razorPage, IUrlHelper helper, IMenuItem menuItem) { TagBuilder liTag = new TagBuilder("li"); if (menuItem.IsSeparator) { liTag.AddCssClass("divider"); liTag.Attributes.Add("role", "separator"); return(liTag); } List <IMenuItem> subItems = menuItem.SubItems.ToList(); TagBuilder aTag = new TagBuilder("a"); if (!menuItem.IsVisible) { liTag.AddCssClass("hidden"); } else if (!menuItem.IsEnabled) { liTag.AddCssClass("disabled"); } else if (!string.IsNullOrWhiteSpace(menuItem.Url)) { aTag.Attributes.Add("href", menuItem.Url); } else if (!string.IsNullOrEmpty(menuItem.Action) && menuItem.Controller != null) { ControllerActionDescriptor actionDescriptor = razorPage.ViewContext.ActionDescriptor as ControllerActionDescriptor; if (actionDescriptor?.ActionName == menuItem.Action && actionDescriptor?.ControllerName == menuItem.Controller) { liTag.AddCssClass("active"); } aTag.Attributes.Add("href", helper.Action(menuItem.Action, menuItem.Controller, menuItem.RouteValues)); } else { aTag.Attributes.Add("href", "#"); } aTag.InnerHtml.Append(menuItem.Name); liTag.InnerHtml.AppendHtml(aTag); if (subItems.Count != 0) { aTag.AddCssClass("dropdown-toggle"); aTag.Attributes.Add("data-toggle", "dropdown"); aTag.Attributes.Add("role", "button"); aTag.Attributes.Add("aria-haspopup", "true"); aTag.Attributes.Add("aria-expanded", "false"); TagBuilder spanCaret = new TagBuilder("span"); spanCaret.AddCssClass("caret"); aTag.InnerHtml.AppendHtml(spanCaret); TagBuilder ulTag = new TagBuilder("ul"); ulTag.AddCssClass("dropdown-menu"); foreach (IMenuItem subItem in subItems) { ulTag.InnerHtml.AppendHtml(WriteMenuItemToBuilder(razorPage, helper, subItem)); } liTag.InnerHtml.AppendHtml(ulTag); } return(liTag); }
/// <summary> /// model验证错误信息 /// </summary> public static IEnumerable <Never.Web.Mvc.Controllers.BasicController.ModelStateError> ModelError(this Microsoft.AspNetCore.Mvc.Razor.RazorPage page) { if (page.TempData["ModelErrorKey"] != null) { return((IEnumerable <Never.Web.Mvc.Controllers.BasicController.ModelStateError>)page.TempData["ModelErrorKey"]); } var list = ModelError(page.ViewContext); if (list != null) { page.TempData["ModelErrorKey"] = list.ToList(); } else { page.TempData["ModelErrorKey"] = new Never.Web.Mvc.Controllers.BasicController.ModelStateError[0]; } return(list); }
/// <summary> /// 获取Model的错误信息 /// </summary> /// <param name="page"></param> /// <returns></returns> public static string ModelErrorMessage(this Microsoft.AspNetCore.Mvc.Razor.RazorPage page) { return(ModelErrorMessage(page.ViewContext)); }