Click here to Skip to main content
15,920,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My Base class:

public class GlobalVariables
   {
       public static HttpClient client = new HttpClient();

       public GlobalVariables()
       {
           client.BaseAddress = new Uri("http://localhost:8080");
           client.DefaultRequestHeaders.Clear();
           client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/xml"));
       }
   }


My Controller:

[HttpGet]
        public ActionResult GetById()
        {
            IEnumerable<StudentViewModel> list;
            HttpResponseMessage response = GlobalVariables.client.GetAsync("/System_Users").Result;
            list = response.Content.ReadAsAsync<IEnumerable<StudentViewModel>>().Result;
            return View(list);
        }



I get the following error:
System.InvalidOperationException: 'An invalid request URI was provided. The request URI must either be an absolute URI or BaseAddress must be set.'


What I have tried:

I have tried adding an @ sign I have also tried adding the full url's in both my base class and controllers
Posted
Updated 3-Jan-19 8:41am

 
Share this answer
 
Comments
ZainNabi 3-Jan-19 13:14pm    
It does not work still and I get the same error
F-ES Sitecore 3-Jan-19 13:38pm    
So google the error message and go through the search results until you find the answer for your issue.
MadMyche 3-Jan-19 14:15pm    
Which solution did you try?
See article here, it also shows some alternatives to using HttpClient:
A Few Great Ways to Consume RESTful API in C# - Code Maze[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900