max
max

Reputation: 645

Getting 404 response from web api

  1. I have webapi and method:

    public class ArticlesController : ApiController
    {
         //
         // GET: api/values
         public IEnumerable<Article> Get()
         {
             using (CollectionsEntities entities = new CollectionsEntities())
             {
                  return entities.Articles.ToList<Article>();
             }
         }
     }
    

It works fine, and check it in browser:

webapi works

  1. I create a client:

    public static class CollectionClient
    {
        private static readonly HttpClient client;
    
        public static Uri ServerBaseUri
        {
            get { return new Uri("http://localhost:10779/api/"); }
        }
    
        public static Boolean IsDirty { get; private set; }
    
        static CollectionClient()
        {
            IsDirty = true;
            client = new HttpClient(new DemoHttpMessageHandler());
        }
    
        // get all articles
        public static async Task<List<Article>> GetAllArticlesAsync()
        {
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            Uri serverUril = new Uri(ServerBaseUri, "Articles");            
            var response = await client.GetAsync(serverUril);
    
            response.EnsureSuccessStatusCode();
    
            var articles = await response.Content.ReadAsAsync<List<Article>>();
            IsDirty = false;
            return articles;   
        }
    }
    

The response gets 404 status code: enter image description here

ID_CAP_NETWORKING is checked to make sure it can access internet. Could anybody give me a hand? Thanks in advance!

Upvotes: 0

Views: 1111

Answers (2)

max
max

Reputation: 645

In windows phone 7.x, the emulator shared the networking of the Host PC, that means: we could host services on our PC and access them from our code using htt://localhost..

In windows phone 8, the emulator is a virtual machine running under Hyper V, that means: you cannot access services on your PC using htt://localhost..

you must use the correct host name or raw IP address of our host PC in URIs

Upvotes: 0

Dmitry S.
Dmitry S.

Reputation: 8503

I only have experience with Android but "localhost" for the emulator is probably different than the "localhost" for your PC. Try using the internal subnet IP instead.

Upvotes: 1

Related Questions