【问题标题】:How to get the urlHelper from html helper context in asp.net core mvc html helper static method如何从 asp.net core mvc html helper static 方法中的 html helper 上下文中获取 urlHelper
【发布时间】:2020-09-30 13:38:49
【问题描述】:

我在ASP.NET 5 RC1 有一个项目,我用它在我的 HTMLHelper 静态方法中从 HtmlHelper 上下文中获取 urlHelper

    public static IHtmlContent MyHtmlHelperMethod<TModel, TResult>(
            this IHtmlHelper<TModel> htmlHelper,
            Expression<Func<TModel, TResult>> expression)
    {



       //get the context from the htmlhelper and use it to get the urlHelper as it isn't passed to the method from the view
        var urlHelper = GetContext(htmlHelper).RequestServices.GetRequiredService<IUrlHelper>();

        var controller = htmlHelper.ViewContext.RouteData.Values["controller"].ToString();
        string myLink;
        if (htmlHelper.ViewContext.RouteData.Values["area"] == null)
        {
            myLink= urlHelper.Action("index", controller);

        } else
        {

            string area = htmlHelper.ViewContext.RouteData.Values["area"].ToString();
            myLink = urlHelper.Action("index", controller, new { area = area });

        }  
        string output = "<div><a href = \"" + myLink + "\" class=\"myclass\"><blabla></blabla>My Link</a></div>;
        return new HtmlString(output.ToString());
    }

但是在ASP.NET Core 中,它不再起作用,并且出现运行时错误

>InvalidOperationException: No service for type 'Microsoft.AspNetCore.Mvc.IUrlHelper' has been registered.

this stackoverflow 答案中发布的解决方案是注入 IUrlHelperFactory,但是我使用的是我在 cshtml 中调用的静态 html 辅助方法,而不是 taghelpers 中使用的类。

如何更改我的代码以在 ASP.net Core 中工作?

【问题讨论】:

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


    【解决方案1】:

    Microsoft 可能在 .net core 2.1 中做了一些改动。它比公认的答案略简单。我希望这对某人有帮助。

    using Microsoft.AspNetCore.Html;
    using Microsoft.AspNetCore.Mvc.Rendering;
    using Microsoft.AspNetCore.Mvc.Routing;
    using Microsoft.Extensions.DependencyInjection;
    
    namespace MyApp.Extensions
    {
        public static class WebApiHelperExtension
        {
            public static IHtmlContent WebApiUrl(this IHtmlHelper htmlHelper)
            {
                var urlHelperFactory = htmlHelper.ViewContext.HttpContext.RequestServices.GetRequiredService<IUrlHelperFactory>();
                var urlHelper = urlHelperFactory.GetUrlHelper(htmlHelper.ViewContext);
                //...
            }
        }
    }
    

    【讨论】:

    • 但是你会从哪里获得 htmlhelper 实例?
    • 它是通过DependencyInjection自动传入的,所以不需要获取。您可以从不带参数的 .cshtml 文件中调用该函数:@Html.WebApiUrl()
    【解决方案2】:

    将您的原始代码更改为:

    var urlHelperFactory = GetContext(htmlHelper).RequestServices.GetRequiredService<IUrlHelperFactory>();
    var actionContext = GetContext(htmlHelper).RequestServices.GetRequiredService<IActionContextAccessor>().ActionContext;
    var urlHelper = urlHelperFactory.GetUrlHelper(actionContext); 
    

    【讨论】:

    • 感谢您的解决方案,但我不得不说它并不漂亮,我希望 asp.net 核心开发人员能够让这更容易一些
    • 我不知道这是否是一个新的 .net core 2.1 的东西,但是上面现在给我一个错误,说 IActionContextAccessor 没有注册。请参阅下面的答案,现在只需两行代码。耶。
    猜你喜欢
    • 1970-01-01
    • 2014-12-09
    • 2010-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多