0

I'm trying to make a call to my .net API controller from my Angular 2 service but I keep getting a 404 that it's not found. I'm able to browse to the api function in the browser and return data just fine (ie: http://localhost:51849/api/Application/Get). Any clue where I'm going wrong? I've done similar work in AngularJS 1 like this with no issue. Any help is appreciated. Thanks!

The Angular 2 service call:

getApplications(): Promise<Application[]> {
      return this.http.get('/api/Application/Get/')
          .toPromise()
          .then(response => response.json().data as Application[])
          .catch(this.handleError);
  }

The .Net API controller function:

public class ApplicationController : ApiController
{
    public HttpResponseMessage Get()
    {
        var applicationsModel = new ApplicationsModel();

        try
        {
            var applications = new ApplicationService().GetAllApplications();

            foreach (var application in applications)
            {
                applicationsModel.Applications.Add(new Application(application.MWF_ApplicationID, (int)application.MWF_Priority, application.MWF_DateCreated.ToString("MM/dd/yyyy"), "", "", 0, 0));
            }

        }
        catch(Exception e)
        {
            applicationsModel.Error = "Error getting applications: " + e.Message;
        }

        return this.Request.CreateResponse(HttpStatusCode.OK, applicationsModel);
    }
}

1 Answer 1

1

For an Api controller you would call this.http.get("/api/Application")

It should automagically figure out that it should call the Get() function for you.

This document is a good read for more information: http://www.asp.net/web-api/overview/web-api-routing-and-actions/routing-in-aspnet-web-api

Sign up to request clarification or add additional context in comments.

4 Comments

I've tried both ways but still get a 404 when calling it.
Have you tried adding the [HttpGet] attribute to the method? Spit balling at this point.
Yes, I have tried it with [HttpGet] on the method but still to no avail. I'm not sure what I'm missing. Especially since I can reach it and get data by directly putting the localhost/api/Application/Get url into the browser.
I managed to get it working. There must have been some remnants from the quickstart code left that were giving me issues. Perhaps.. Either way, thanks for the help. :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.