asp.net-mvc 如何在控制器外访问 RequestContext?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3722352/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 00:29:50  来源:igfitidea点击:

How do I Access the RequestContext Outside the Controller?

asp.net-mvcdependency-injectionurlhelperrequestcontext

提问by nfplee

Background

背景

I am trying to move business logic out from the controllers into their own services.

我试图将业务逻辑从控制器移出到他们自己的服务中。

Controller

控制器

public class AccountController : Controller
{
    private readonly IAccountService _accountService; 

    public AccountController(IAccountService accountService)
    {
        _accountService = accountService;
    }

    ....
}

I'm using Unity to inject dependencies. I'd like to use the Url.GenerateUrl()helper method within the implementation of IAccountServicebut Urlis a property against the controller.

我正在使用 Unity 注入依赖项。我想Url.GenerateUrl()在实现中使用helper 方法,IAccountServiceUrl它是针对控制器的一个属性。

I looked at the MVC source to see how this is done but it requires me to access the RequestContextfrom outside of the controller, and I don't know how to do that.

我查看了 MVC 源代码以了解这是如何完成的,但它要求我RequestContext从控制器外部访问,而我不知道该怎么做。

Question

How do I access the RequestContext from outside the controller? If that won't solve my problem, how do I solve the problem given my setup?

如何从控制器外部访问 RequestContext?如果这不能解决我的问题,我该如何解决给定我的设置的问题?

采纳答案by Darin Dimitrov

However i'd like to use the Url.GenerateUrl helper methods within my implementation of IAccountService

但是我想在我的 IAccountService 实现中使用 Url.GenerateUrl 辅助方法

Simply pass this information as parameter. Example:

只需将此信息作为参数传递即可。例子:

public ActionResult Index()
{
    var someUrl = Url.Action("about");
    _accountService.Foo(someUrl);
}

Now you no longer need UrlHelper inside your service classes. Everything that needs interacting with MVC infrastructure shouldn't be placed in your service classes. They shouldn't depend on any Request, Response, Session, ... It's the controller's responsibility to work with those objects and glue them together with your service classes.

现在,您的服务类中不再需要 UrlHelper。所有需要与 MVC 基础设施交互的东西都不应该放在你的服务类中。它们不应该依赖于任何请求、响应、会话……控制器有责任处理这些对象并将它们与您的服务类粘合在一起。

回答by LukeH

This might not be quite right because I'm unable to test it at the moment, but I think that you can do something like this in .NET 4+:

这可能不太正确,因为我目前无法对其进行测试,但我认为您可以在 .NET 4+ 中执行以下操作:

using System.Web;
using System.Web.Mvc;

// ...

var helper = new UrlHelper(HttpContext.Current.Request.RequestContext);
string url = helper.GenerateUrl(/* ... */);

It mightmake more sense to pass the context from the controller to your IAccountServiceimplementation rather than grabbing it directly from HttpContext.Current.

可能会更有意义,从控制器的情况下传递到您的IAccountService实现,而不是直接从抓住它HttpContext.Current