best practice to configure baseaddress URI in MVC Core

Aamiz 21 Reputation points
2020-12-02T01:24:39.33+00:00

Hi I have created an MVC and Web Api project. When consuming web Api endpoints I am using the complete Uri as showing below.

string apiUrl = "http://localhost:57025/api/Mypage;

ON every single data class I am keep adding the full uri whenever I am consuming webapi. What would be the location where I can configure base uri . like
http://localhost:57025/api. So change of Uri can be easy.

Thanks

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,059 questions
ASP.NET API
ASP.NET API
ASP.NET: A set of technologies in the .NET Framework for building web applications and XML web services.API: A software intermediary that allows two applications to interact with each other.
284 questions
0 comments No comments
{count} votes

Accepted answer
  1. Yihui Sun-MSFT 801 Reputation points
    2020-12-04T06:45:34.283+00:00

    Hi @Aamiz ,
    You could use the IHttpClientFactory I mentioned above (I gave the link in the previous reply.).

    1. Use the Named clients method of IHttpClientFactory.
    2. Click on the link to view the detailed code, or you can view the code I copied from the link to the following.

    Configuration for a named HttpClient can be specified during registration in Startup.ConfigureServices

    services.AddHttpClient("myapi", c =>  
    {  
        c.BaseAddress = new Uri("http://localhost:57025/api/");  
    });  
    

    To create a named client, pass its name into CreateClient

     var request = new HttpRequestMessage(HttpMethod.Get,"Mypage");  
     var client = _clientFactory.CreateClient("myapi");  
     var response = await client.SendAsync(request);  
    

    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best Regards,
    YihuiSun

    1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. Yihui Sun-MSFT 801 Reputation points
    2020-12-02T07:24:31.29+00:00

    Hi @Aamiz ,
    Do you want to set the base address of the URI used when sending requests in ASP.NET Core MVC?
    How did you request the API?If you are using HttpClient, you only need to set HttpClient.BaseAddress.

    HttpClient client = new HttpClient();  
    client.BaseAddress = new Uri("http://localhost:57025/api/");  
    var response = await client.GetAsync("Mypage");  
    

    If you use IHttpClientFactory in ASP.NET Core to make HTTP requests, you can refer to this official link.


    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best Regards,
    YihuiSun


  2. Armin Shoeibi 1 Reputation point
    2020-12-03T21:54:29.663+00:00

    Hello, I did this in a project, I will provide a link for that project.
    https://github.com/ArminShoeibi/WeatherAPI
    https://github.com/ArminShoeibi/WeatherAPI/blob/master/WeatherService.cs
    * Please use IHttpClientFactory when consuming APIs.

    0 comments No comments