我如何访问RequestContext的控制器之外? [英] How do I Access the RequestContext Outside the Controller?

查看:127
本文介绍了我如何访问RequestContext的控制器之外?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景

我想从控制器移出业务逻辑转化为自己服务。

控制器

 公共类的AccountController:控制器
{
    私人只读IAccountService _accountService;    公众的AccountController(IAccountService的AccountService)
    {
        _accountService =帐户服务;
    }    ....
}

我使用统一注入的依赖。我想使用 IAccountService 的实现,但<$ C $在 Url.GenerateUrl() helper方法C>网址是对控制器的属性。

我看了看MVC源代码,看看如何做到这一点,但它要求我从控制器的外部访问 RequestContext的,我不知道该怎么做到这一点。

问题

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


解决方案

  

不过,我想用我的执行IAccountService的

中的Url.GenerateUrl辅助方法

简单地传递这些信息作为参数。例如:

 公众的ActionResult指数()
{
    VAR someUrl = Url.Action(关于);
    _accountService.Foo(someUrl);
}

现在您不再需要UrlHelper服务类中。需要与MVC基础设施交互的一切不应该被放置在你的服务类。他们不应该依赖于任何请求,响应会议,...这是控制器的责任与这些对象的工作,并与您的服务类粘合在一起他们。

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?

解决方案

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.

这篇关于我如何访问RequestContext的控制器之外?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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