jbassking10
jbassking10

Reputation: 803

Can't call Web api from Xamarin

I'm trying to make a simple web api call using HttpClient in an iOS build. When setting the client.BaseAddress, the BaseAddress always ends up null. I can't find what could possibly be wrong.

    using System.Net.Http;
     ....
    private const string BaseApiUrl = "http://localhost:55904/";
    static HttpClient client = new HttpClient();
    client.BaseAddress = new Uri(BaseApiUrl);
    // client.BaseAddress is still null

I downloaded a sample non-Xamarin project and the same code works fine. Any ideas?

Update As I continue messing with this I figured I would try some of the other platforms. UWP seems to work fine. I was going to test Android but all of a sudden, I have a bunch of "The name 'InitializeComponent' does not exist in the current context" errors. I'm new to Xamarin but not to VS or C#.

Update I was just thinking, does iOS need to request any special permission for internet access? If so, where would that be set?

Upvotes: 0

Views: 933

Answers (1)

jbassking10
jbassking10

Reputation: 803

Just in case anyone else comes across this problem, I found the cause. It turns out that the iOS project didn't include a reference to System.Net.Http. Once I added the reference, everything worked fine.

Upvotes: 1

Related Questions