5

I am looking for a simple way (if it exists) to generate a URL in a service class for a specified controller and action in an ASP.NET MVC 3 web application. I want to do this in the service layer because of needing to encode this URL in a QR code.

This is simple in a View or in a controller because of the UrlHelper available through System.Web.Mvc so I could create the beginning of the URL in the controller action that uses my service class but I was hoping to do it at the point of QR code generation.

Thanks in advance.

1
  • what code have you tried yourself so far.. can you post what you have tried..? Commented Aug 3, 2012 at 17:09

3 Answers 3

11

A major reason for having distinct layers is separation of concerns. The service layer should not concern itself with request routing.

I would pass the URL into the service layer and have the service layer return the generated QR code.

Sign up to request clarification or add additional context in comments.

4 Comments

Yep, Use the Url helper in the controller, and pass it to the service layer as argument.
Absolutely exactly precisely what I was thinking.
I wasn't sure if that was best or not. Thank you for your clarification.
What if you have to send an email with a link to the application's endpoint, then you would need to construct the link in the service layer!
5

I understand the separation of concerns but sometimes a developer has to do what a developer has to do!

So here is what I have done:

var Url = new UrlHelper(HttpContext.Current.Request.RequestContext);
var myUrl = Url.RouteUrl("routeName", new { controller="ControllerName", action="Index", area="", otherDictionaryEntries="otherId"});

1 Comment

This works only in the case when the service is used from a controller. Lets say you have a background task to send emails and you need the link inside that email... in that case you do not have a request or any http context.
0
private readonly LinkGenerator _urlHelper;
private readonly IHttpContextAccessor _httpContextAccessor;

public MyUrlService(LinkGenerator urlHelper, IHttpContextAccessor httpContextAccessor)
{
    _urlHelper = urlHelper;
    _httpContextAccessor = httpContextAccessor;
}

public string MyCreateUrl() 
{ 
    var url = _urlHelper.GetUriByAction("Index1", "Home", new { param1 = "myparam1", param2 = "myparam2" }, _httpContextAccessor.HttpContext!.Request.Scheme, _httpContextAccessor.HttpContext.Request.Host); 
}

Url output:

"http://localhost:5241/Home/Index1?param1=myparam1&param2=myparam2"

We do not forget to add our services via Program.cs

builder.Services.AddHttpContextAccessor();

1 Comment

This works only in the case when the service is used from a controller. Lets say you have a background task to send emails and you need the link inside that email... in that case you do not have a request or any http context.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.