How do I Access the RequestContext Outside the Controller?
Asked Answered
A

2

21

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?

Argillaceous answered 15/9, 2010 at 22:30 Comment(0)
C
19

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.

Cyd answered 16/9, 2010 at 6:36 Comment(2)
Hi, i think this is the way to go. ThanksArgillaceous
This should receive more upvotes. The HttpContext should not be used within the service layer (as the answer below suggest). What happens when you want to use your service in an application that doesn't have access to an HttpContext, or a Restful API that has a different endpoint URL?Dotterel
C
45

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.

Coordinate answered 15/9, 2010 at 22:38 Comment(4)
As @Coordinate suggests, it is much easier to pass an instance of the UrlHelper to your AccountService.Glochidiate
@user155899: Presumably you're using .NET 3.5 then? I think the RequestContext property was introduced in .NET 4.Coordinate
@user155899: No worries. Darin's answer is the better one anyway.Coordinate
@Argillaceous - I made a minor edit so that you could change your vote.Vegetal
C
19

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.

Cyd answered 16/9, 2010 at 6:36 Comment(2)
Hi, i think this is the way to go. ThanksArgillaceous
This should receive more upvotes. The HttpContext should not be used within the service layer (as the answer below suggest). What happens when you want to use your service in an application that doesn't have access to an HttpContext, or a Restful API that has a different endpoint URL?Dotterel

© 2022 - 2024 — McMap. All rights reserved.