Angular 2从同一个项目调用.Net控制器

时间:2016-09-30 14:36:05

标签: .net angularjs asp.net-web-api angular

我正在尝试从我的Angular 2服务调用我的.net API控制器,但是我一直收到404它没有找到它。我能够浏览到浏览器中的api函数并返回数据(即:http://localhost:51849/api/Application/Get)。我出错的任何线索?我在AngularJS 1中做过类似的工作,没有问题。任何帮助表示赞赏。谢谢!

Angular 2服务电话:

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

.Net API控制器功能:

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 个答案:

答案 0 :(得分:1)

对于Api控制器,您可以拨打this.http.get("/api/Application")

它应该 自动判断它应该为你调用Get()函数。

本文档适用于更多信息: http://www.asp.net/web-api/overview/web-api-routing-and-actions/routing-in-aspnet-web-api