使用AngularJS使用Parameters调用Web ApI方法

时间:2016-10-06 18:07:58

标签: angularjs angularjs-service asp.net-web-api-routing

我正在使用angularJS Service调用Web API。

我的webp API方法工作正常,没有参数'/api/Subpart/'

当我尝试传递参数时,Web API方法没有触发。

角度服务

.service('subpartService', ['$http', function ($http) {
     this.getSubparts = function (val) {
       return $http.get('/api/Subpart/'+val); // With parameters not working
     };
 }])

Web API控制器操作

[Route("api/Subpart/{strsubpart}")]
public List<tbl_Employee> GetSubparts(string strsubpart)
{
    Entities db = new Entities();
    var data = db.tbl_Regulation.Where(c => c.Subsection == "ABC");
    return data.ToList();
}

2 个答案:

答案 0 :(得分:0)

我有解决方案。它有传递参数吗?

log.txt

答案 1 :(得分:0)

尝试“逃避”该值。

http://www.w3schools.com/jsref/jsref_encodeuri.asp

this.getSubparts = function (val) {
           var subUrl = "http://localhost/api/Subpart/?subpart="+ encodeURI(val);
             return $http.get(subUrl);
         };