。Net Core 3.0 HTML Helper

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了。Net Core 3.0 HTML Helper相关的知识,希望对你有一定的参考价值。

[嗨,我有一个以前使用过的html帮助器。我想在.net core 3.0中使用相同的帮助程序,但它给出了一个错误。我以前从未使用过内核,所以我正在做一个新项目来找出答案。我找不到对应的代码。你能帮助我吗。预先感谢您的帮助

  public static class HtmlHelperExt
    
        public static string IsSelected(this HtmlHelper html, string controllers = "", string actions = "", string cssClass = "selected")
        
            ViewContext viewContext = html.ViewContext;
            bool isChildAction = viewContext.Controller.ControllerContext.IsChildAction;

            if (isChildAction)
                viewContext = html.ViewContext.ParentActionViewContext;

            RouteValueDictionary routeValues = viewContext.RouteData.Values;
            string currentAction = routeValues["action"].ToString();
            string currentController = routeValues["controller"].ToString();

            if (String.IsNullOrEmpty(actions))
                actions = currentAction;

            if (String.IsNullOrEmpty(controllers))
                controllers = currentController;

            string[] acceptedActions = actions.Trim().Split(',').Distinct().ToArray();
            string[] acceptedControllers = controllers.Trim().Split(',').Distinct().ToArray();

            return acceptedActions.Contains(currentAction) && acceptedControllers.Contains(currentController) ?
                cssClass : String.Empty;
        

        public static string ContentArea(this UrlHelper url, string path)
        
            var modulName = url.RequestContext.RouteData.DataTokens["area"];
            string modulContentLoad = "";

            if (modulName != null)

            
                if (!string.IsNullOrEmpty(modulName.ToString()))
                    modulContentLoad = "Areas/" + modulName;

                if (path.StartsWith("~/"))
                    path = path.Remove(0, 2);

                if (path.StartsWith("/"))
                    path = path.Remove(0, 1);


                path = path.Replace("../", string.Empty);

                return VirtualPathUtility.ToAbsolute("~/" + modulContentLoad + "/" + path);
            

            return string.Empty;
        
    

我的屏幕截图和代码在下面

“在此处输入链接描述”

答案

单击帮助文本,然后选择Microsoft.AspNetCore.Mvc.ViewFeatures名称空间。

另一答案

我真正想做的是标记我所在的屏幕。我解决了问题。我添加了运行代码。

enter image description here

net.core 3.0使用的版本

 public static class HtmlHelperExt
    
        public static string IsSelected(this IHtmlHelper html, string controllers = "", string actions = "", string cssClass = "selected")
        
            ViewContext viewContext = html.ViewContext;
            bool isChildAction = viewContext.ClientValidationEnabled;

            if (isChildAction)
                viewContext = html.ViewContext;

            RouteValueDictionary routeValues = viewContext.RouteData.Values;
            string currentAction = routeValues["action"].ToString();
            string currentController = routeValues["controller"].ToString();

            if (String.IsNullOrEmpty(actions))
                actions = currentAction;

            if (String.IsNullOrEmpty(controllers))
                controllers = currentController;

            string[] acceptedActions = actions.Trim().Split(',').Distinct().ToArray();
            string[] acceptedControllers = controllers.Trim().Split(',').Distinct().ToArray();

            return acceptedActions.Contains(currentAction) && acceptedControllers.Contains(currentController) ?
                cssClass : String.Empty;
        

        public static string ContentArea(this UrlHelper url, string path)
        
            var modulName = url.ActionContext.RouteData.DataTokens["area"];
            string modulContentLoad = "";

            if (modulName != null)

            
                if (!string.IsNullOrEmpty(modulName.ToString()))
                    modulContentLoad = "Areas/" + modulName;

                if (path.StartsWith("~/"))
                    path = path.Remove(0, 2);

                if (path.StartsWith("/"))
                    path = path.Remove(0, 1);


                path = path.Replace("../", string.Empty);

                return VirtualPathUtility.ToAbsolute("~/" + modulContentLoad + "/" + path);
            

            return string.Empty;
        
    

以上是关于。Net Core 3.0 HTML Helper的主要内容,如果未能解决你的问题,请参考以下文章

在 ASP.NET Core MVC 中选择 ENUM Tag Helper

.NET Core 3.0及ASP.NET Core 3.0 前瞻

在 Elastic Beanstalk 上的 Asp.net core 3.0

.net core 3.0和.net5有什么区别

ASP.NET Core Razor Pages 强制 Anchor Tag Helper (asp-page) 使用小写路由

从.Net core 2.2迁移到.Net Core 3.0