Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them, it only takes a minute:

I've built a web api for my mobile app. I test it on the localhost in browser and it returns json as expected, but whenever I host it on azure and hit the same URL I receive no data, but google network (developer console) in chrome says it has retrieved 408B of data, but no content shows up where the json should be present?

What could I not have configured correctly?

I have gone into the Global.ascx file and added the following line of code

 public static void Register(HttpConfiguration config)
    {
        config.EnableCors();
    }

and added the EnableCors attribute on top of the webapi controller class and set the prefix router too.

What am I missing?

Controller code see below for a snippet of it. the database is an EF Code First generated from a database that already existed. Please note.

 [EnableCors(origins: "*", headers: "*", methods: "*")]
    [RoutePrefix("api/numbers")]
    [AllowAnonymous]
    public class NumbersController : ApiController
    {
        private MegaMillions db = new MegaMillions();

        [HttpGet]
        public HttpResponseMessage Get()
        {
            return db.Numbers.Take(10).ToList();
        }

        // GET: api/Numbers
        public List<Number> GetNumbers()
        {
            return db.Numbers.ToList();
        }
}           

if I can figure out how to get the list working the other CRUD operations will follow suit.

share|improve this question

1 Answer 1

[EnableCors(origins: "*", headers: "*", methods: "*")]
[RoutePrefix("api/values")]
public class ValuesController : ApiController
{
    List<Doctor> doctors = new List<Doctor>(){ new Doctor { Id = 1, FirstName = "Carl Finch", LastName = "Finch" }, new Doctor { Id = 2, FirstName = "Carl", LastName = "Finch" } };
    // GET api/values/5
    [Route("Get")]
    public string Get()
    {
        return "This is my controller response";
    }

    [Route("GetNumbers")]
    public List<Doctor> GetNumbers()
    {
        return doctors;
    }
}

So after adding Routes to each method in the web api it worked just fine!

So remember declaring the RouterPrefix is NOT ENOUGH to get data, each method in a web api has to have a proper Router attribute.

share|improve this answer
    
Please someone edit this if I am mistaken, but I was able to receive data after testing on azure server. – Carl Finch Feb 23 at 7:26

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.