3

How can I get my API URL (only API URL).

In development mode https://localhost:44372/

In production mode: https://{{ server api url }}

This is because I want to store full path file in Database some like this:

https://localhost:44372/files/licenceFiles/1234.png

4 Answers 4

2

Well, I used the follwing:

    private readonly IUnitOfWork _unitOfWork;
    private readonly IHttpContextAccessor _httpAccessor;
    private string _serverPath;

    public CustomerLicenceFilesService(IUnitOfWork unitOfWork, IHttpContextAccessor httpAccessor)
    {
        _unitOfWork = unitOfWork;
        _httpAccessor = httpAccessor;
        _serverPath = $"{_httpAccessor.HttpContext.Request.Scheme}://{_httpAccessor.HttpContext.Request.Host}";
    }
Sign up to request clarification or add additional context in comments.

2 Comments

Well, someone put unlike, but I already use this and its working in several environments.
I appreciate! This really helped me, @Marcelo: var url = $"{_httpAccessor.HttpContext.Request.Scheme}://{_httpAccessor.HttpContext.Request.Host}";.
1

This depends on where you configure your production Url. There are roughly half a dozen places, where you can store them out of the box.

LazZiya has given you an approach that works just fine locally, but hits some limitations, once you exceed the scenario, where there is only dev and prod.

If you configure them in your appsettings.json and as an environment variable, the environment variable take have precedence. If your app is going to be containerized, chances are, that most of these values will be set using einvironment variables.

To read those use Environment.GetEnvironmentVariable("ASPNETCORE_URLS"), which would give you the internal urls Kestrel is listening on. public addresses, will probably have a different key - but you get the idea.

You can read up on this here: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-6.0

2 Comments

Ok, but there is no HttpContext.GetUrl or some like this?
You can get the requst path from http context: learn.microsoft.com/de-de/dotnet/api/…
1

Define both URL's in appsettings.json:

{
    "ApiUrl_local": "https://localhost:xxx",
    "ApiUrl_web": "https://website.com";
}

Then use IWebHostEnvironment to check for development mode and retrive the relevant setting:

public class ApiConfig
{
    public IWebHostEnvironment Environment { get; }
    public IConfiguration Configuration { get; }


    public ApiConfig(IWebHostEnvironment environment, IConfiguration configuration)
    {
        Environment = environment;
        Configuration = configuration
    }

    public string ApiURL => Environment.IsDevelopment() ? 
    Configuration["ApiUrl_local"] : Configuration["ApiUrl_web"];
}

4 Comments

While this may work locally, it will become tedious to use, once you containerize the application and use it in multiple stages (test, user acceptance, dev prod, ...). Personally I'd prefer a configuration over Environment variables, which you can set using build variables in your container image
@Marco yes you are right, I just used a simple usecase :)
No worries. Up to the OP to decide, if the approach is feasible for his scenario.
There is no HttpContext.GetUrl or some like this?
0

I found the solution using

$"{Request.Scheme}://{Request.Host}{Request.PathBase}{Request.Path}{Request.QueryString}"

The answer is thanks to Niels Swimberghe, and it is available on https://swimburger.net/blog/dotnet/how-to-get-the-full-public-url-of-aspdotnet-core

Comments

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.