7

I've just recently switched over from ASP.NET MVC to using .Core 2 and I can't figure out how to get the current URL in Core. I could get it easily enough using the Request in previous asp.net versions, but since that's no long valid in .Net Core I'm at a loss.

I haven't been able to find any way from my google searching as of now.

Any help is appreciated. Thanks!

Memedon
  • 907
  • 2
  • 7
  • 14
  • Possible duplicate of https://stackoverflow.com/questions/43526630/how-can-i-get-the-baseurl-of-my-site-in-asp-net-core – fasaas Oct 11 '18 at 21:30
  • Didn't end up being a duplicate of that once since it requires an MVC controller which I'm not using. Thank you though. – Memedon Oct 11 '18 at 22:07

2 Answers2

16

In the ConfigureServices method of your Startup.cs file, add the line:

services.AddHttpContextAccessor();

and you will now have access to the IHttpContextAccessor interface throughout your code when using dependency injection.

Usage as follows:

public class CustomerRepository
{
    private readonly IHttpContextAccessor _context;

    public CustomerRepository(IHttpContextAccessor context)
    {
        _context = context;
    }

    public string BaseUrl()
    {
        var request = _context.HttpContext.Request;
        // Now that you have the request you can select what you need from it.
        return string.Empty;
    }
}

Hope this answers your question :)

Nae
  • 14,209
  • 7
  • 52
  • 79
Christopher Lake
  • 358
  • 4
  • 10
  • 1
    Thanks for this, it solved my problem. I do have another question directly related to this, is there a way to get the request in the `Startup.cs`? I'm trying to set configuration variables so I only ever need to grab the request once. – Memedon Oct 11 '18 at 22:05
  • 1
    Funny you should ask, I was trying to do the same thing myself a couple weeks back. Unfortunately it seems that you cannot use dependency injection anywhere in `Startup.cs` because when the `Startup` constructor gets hit it will have no idea what IHttpContextAccessor is yet so you can't pass it through that way. Hopefully someone smarter than myself will be able to provide to prove me wrong or provide a more concise answer :) – Christopher Lake Oct 11 '18 at 22:11
  • Having it working at all is better than nothing. I appreciate your help, and while this is ugly, it works. Thanks! – Memedon Oct 11 '18 at 22:13
  • 1
    As an afterthought, perhaps you could take a look at writing your own Middleware class (there's plenty of good reading material online) which gets added to the application pipeline. This allows you to access requests and responses as soon as they happen. You wire it up by calling `app.UseMiddleware();` in the `Configure` method. – Christopher Lake Oct 11 '18 at 22:17
  • You will also need a `using Microsoft.AspNetCore.Http;` statement and if you are building your service class in a separate project you will need to add the following framework reference to your project file. ` ` – Terence Golla Sep 21 '20 at 20:07
7

Try:

public class Startup {
    public Startup(IConfiguration configuration, IHostingEnvironment env) {
        Configuration = configuration;
        HostingEnvironment = env;
        var url = configuration[WebHostDefaults.ServerUrlsKey];
   }

Please note that you can get more than one url.

f-CJ
  • 4,235
  • 2
  • 30
  • 28
Anton Dremin
  • 201
  • 3
  • 5