无法利用UrlHelper [英] Unable to utilize UrlHelper

查看:290
本文介绍了无法利用UrlHelper的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试在ASP.NET 4中做一些简单而直接的事情,但是现在在ASP.NET 5中却不是这种情况.

I'm currently trying to do something that was dead simple and straight forward in ASP.NET 4 however this ins't the case now in ASP.NET 5.

以前使用UrlHelper非常简单:

Previously to use the UrlHelper it was dead simple:

var urlHelper = new UrlHelper(HttpContext.Current.Request.RequestContext);

但是,我一生都无法解决如何使用新的UrlHelper的问题.我正在看测试用例,或者我完全傻了,或者我遗漏了一些东西,但似乎无法弄清楚.在此方面提供的任何帮助都将非常有用.

However I can't for the life of me wrap my head around how to use the new UrlHelper. I'm looking at the test cases and either I'm completely daft or I'm missing something and I can't seem to figure it out. Any help here in clearing up this would be great.

推荐答案

更新-RC2发布

如@deebo所述,您不再可以直接从DI获取IUrlHelper.相反,您需要在类中注入IUrlHelperFactoryIActionContextAccessor,并使用它们来获取IUrlHelper实例,如下所示:

As @deebo mentioned, you no longer can get an IUrlHelper directly from DI. Instead you need to inject an IUrlHelperFactory and an IActionContextAccessor into your class and use them to get the IUrlHelper instance as in:

public MyClass(IUrlHelperFactory urlHelperFactory, IActionContextAccessor actionAccessor)
{
    this.urlHelperFactory = urlHelperFactory;
    this.actionAccessor = actionAccessor;
}

public void SomeMethod()
{
    var urlHelper = this.urlHelperFactory.GetUrlHelper(this.actionAccessor.ActionContext);
}

您还需要在启动类中注册(默认情况下已经注册了IUrlHelperFactory):

You need to also register the in your startup class (IUrlHelperFactory is already registered by default):

services.AddSingleton<IActionContextAccessor, ActionContextAccessor>();

请记住,只有在MVC/路由中间件之后运行actionContext的代码才能运行,这才有效! (否则actionAccessor.ActionContext将为空)

Bear in mind this will only work as long as the code where you get the actionContext is running after the MVC/routing middleware! (Otherwise actionAccessor.ActionContext would be null)

我已经使用HttpContext.RequestServices中的IServiceProvider检索了IUrlHelper.

I have retrieved the IUrlHelper using the IServiceProvider in HttpContext.RequestServices.

通常,您将拥有一个HttpContext属性:

Usually you will have an HttpContext property at hand:

  • 在控制器操作方法中,您可以执行以下操作:

  • In a controller action method you can do:

var urlHelper = this.Context.RequestServices.GetRequiredService<IUrlHelper>();
ViewBag.Url = urlHelper.Action("Contact", "Home", new { foo = 1 });

  • 在过滤器中,您可以执行以下操作:

  • In a filter you can do:

    public void OnActionExecuted(ActionExecutedContext context)
    {
        var urlHelper = context.HttpContext.RequestServices.GetRequiredService<IUrlHelper>();
        var actionUrl = urlHelper.Action("Contact", "Home", new { foo = 1 });
        //use actionUrl ...
    }
    

  • 另一种选择是利用内置的依赖注入,例如,您的控制器可以具有如下构造函数,并在运行时提供IUrlHelper实例:

    Another option would be taking advantage of the built-in dependency injection, for example your controller could have a constructor like the following one and at runtime an IUrlHelper instance will be provided:

    private IUrlHelper _urlHelper;
    public HomeController(IUrlHelper urlHelper)
    {
        _urlHelper = urlHelper;
    }
    

    这篇关于无法利用UrlHelper的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆