如何在控制器外部访问RequestContext?

发布于 2024-09-19 07:28:11 字数 557 浏览 11 评论 0 原文

背景

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

控制器

public class AccountController : Controller
{
    private readonly IAccountService _accountService; 

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

    ....
}

我正在使用 Unity 来注入依赖项。我想在 IAccountService 的实现中使用 Url.GenerateUrl() 辅助方法,但 Url 是针对控制器的属性。

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

问题

如何从控制器外部访问RequestContext?如果这不能解决我的问题,那么根据我的设置如何解决问题?

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 IAccountService but Url is a property against the controller.

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眼趣 2024-09-26 07:28:37

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

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

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

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

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

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.

╰沐子 2024-09-26 07:28:32

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

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

// ...

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

传递上下文可能更有意义从控制器到您的 IAccountService 实现,而不是直接从 HttpContext.Current

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+:

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

// ...

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

It might make more sense to pass the context from the controller to your IAccountService implementation rather than grabbing it directly from HttpContext.Current.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文