在控制器上找不到与请求angularjs GET匹配的操作

时间:2016-05-27 20:49:43

标签: angularjs asp.net-web-api

有人可以帮助我,我错过了什么或做错了什么。我收到了以下错误:

{"Message":"No HTTP resource was found that matches the request URI 'http://localhost:52096/api/Wotc/GetWotcDashBoardSummary?0=9&1=4&2=-&3=3&4=3&5=8&6=5&7=6&8=6&9=6'.","MessageDetail":"No action was found on the controller 'WotcAPI' that matches the request."}

wotcDashBoard.js:

var WotcDashBoardModule = angular.module('WotcDashBoardModule', []);

WotcDashBoardModule.factory('WotcDashBoardModuleService', ['$http', '$window', function ($http, $window) {

    return {
        GetDashBoardSummary: function (Ein) {
            return $http({
                url: '/api/Wotc/GetWotcDashBoardSummary',
                method: 'GET',
                params: Ein
            });
        }       
    };
}]);


WotcDashBoardModule.controller('WotcDashBoardController', ['$scope', 'WotcDashBoardModuleService', function ($scope, WotcDashBoardModuleService) {

    var Ein = '00-00000';
    WotcDashBoardModuleService.GetDashBoardSummary(Ein).then(function (response) {
         $scope.Summary = response.data.Employees;
    });

}]);

WotcAPIController:

  [RoutePrefix("api/Wotc")]
    public class WotcAPIController : ApiController
    {
        [HttpGet]
        [Route("GetWotcDashBoardSummary")]
        public async Task<IHttpActionResult> GetWotcDashBoardSummary(string Id)
        {
            CompanyWotcBO wotcSummary = new CompanyWotcBO();

            try
            {wotcSummary = await CompanyWotcBL.GetDashBoardSummary(Id);}
            catch
            {}
            return Ok(new { WotcSummary = wotcSummary });
        }
    }

Route.config:

 public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "Jobs",
                url: "Jobs/{id}",
                defaults: new { controller = "Jobs", action = "ShowJobPosting", id = UrlParameter.Optional }
            );

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Account", action = "HomeInit", id = UrlParameter.Optional }
            );
        }
    }

2 个答案:

答案 0 :(得分:1)

这是因为参数名称不匹配

在wotcDashBoard.js中将params: Ein更改为params: { id: ein }

代码:

return {
        GetDashBoardSummary: function (Ein) {
            return $http({
                url: '/api/Wotc/GetWotcDashBoardSummary',
                method: 'GET',
                params: { id: ein }
            });
        }       
    };

答案 1 :(得分:0)

我认为有些事情需要解决:

首先:代替url: '/api/Wotc/GetWotcDashBoardSummary',,它应该类似于url: 'http://www.example.com/api/Wotc/GetWotcDashBoardSummary/id',,其中example.com是您托管服务的域。

其次:

[RoutePrefix("api/Wotc")]
public class WotcAPIController : ApiController
{
    [HttpGet]
    [Route("GetWotcDashBoardSummary/{id}")]
    public async Task<IHttpActionResult> GetWotcDashBoardSummary(string Id)
    {
        CompanyWotcBO wotcSummary = new CompanyWotcBO();

        try
        {wotcSummary = await CompanyWotcBL.GetDashBoardSummary(Id);}
        catch
        {}
        return Ok(new { WotcSummary = wotcSummary });
    }
}

如果您遇到任何跨源请求错误,则可能必须在执行此操作后启用CORS