使用angularJS asp.net网站API调用自定义方法 [英] asp.net web api call custom method using angularJS

查看:220
本文介绍了使用angularJS asp.net网站API调用自定义方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个angularJS控制器,在这里我把剑道级联dropdownlist.For下拉列表值,对剑道数据源读取我打电话网页API服务。
对于第一场的API GetDivisions()被调用,它的响应,但也为第二值GetCascadeDistrict()方法不叫GetDivisions又称为()方法。我该如何解决this.Need帮助​​

在这里与剑道级联的DropDownList角控制器。

  app.controller(filterCtrl功能($范围,SCE $,$ HTTP){
VAR我;
$ scope.dashImgSrc = $ sce.trustAsResourceUrl('内容/图片/ Bangladesh_Govt.gif');$(文件)。就绪(函数(){    VAR师= $(#告)。kendoComboBox({
        过滤:包含,
        占位符:选择一个部门......
        dataTextField:姓名,
        dataValueField:ID,
        动画: {
          关: {
              影响:变焦:去,
              持续时间:250
          }
        },
        数据源: {
            类型:JSON
            serverFiltering:真实,
            运输: {
                解读:API / AreaService / GetDivisions()
            }
        },
        变化:功能(){
            I = divisions.value();
            警报(第一个打+ I);
        }
    })的数据(kendoComboBox);
    VAR区= $(#区)。kendoComboBox({
        autoBind:假的,
        cascadeFrom:师,
        过滤:包含,
        占位符:选择地区,
        dataTextField:姓名,
        dataValueField:ID,
        数据源: {
            类型:JSON
            serverFiltering:真实,
            运输: {
                阅读:功能(){                    警报(第二击);                    // $ http.get(/ API / AreaService / GetCascadeDistrict(I))。成功(函数(){
                    //警报(打区API);
                    //})错误(函数(){
                    //警报(错误);
                    //});                    $ HTTP({方法:GET,网址:'API / AreaService / GetCascadeDistrict(我)'})。
                        成功(函数(){
                            警报(实际上,它击中了custome get方法);
                        })。
                        错误(函数(){
                            警报(打不或其他问题);
                        });
                }
            }
        }
    })的数据(kendoComboBox);
    变种upazila = $(#upazila)。kendoComboBox({
        autoBind:假的,
        cascadeFrom:区,
        过滤:包含,
        占位符:选择一个upazila ......
        dataTextField:姓名,
        dataValueField:ID,
        数据源: {
            类型:JSON
            serverFiltering:真实,
            运输: {
                阅读:功能(){
                    $ http.get(/ API / AreaService / GetCascadeDistrict(I))。成功(函数(){                    })错误(函数(){                    });
                }
            }
        }
    })的数据(kendoComboBox);    $(#GET)。点击(函数(){
        VAR divisionInfo =\\部:{ID:+ divisions.value()+,名称:+ divisions.text()+},
            districtInfo =\\ nDistrict:{ID:+ districts.value()+,名称:+ districts.text()+},
            upazilaInfo =\\ nUpazila:{ID:+ upazila.value()+,名称:+ upazila.text()+};        警报(道路的详细信息:\\ n+ divisionInfo + districtInfo + upazilaInfo);
    });
});

});

和网络API是这里

 公共类AreaServiceController:ApiController
{
    私人只读AreaFilterManager _db =新AreaFilterManager();    [System.Web.Http.HttpGet]
    公共IEnumerable的<&司GT; GetDivisions()
    {
        返回_db.GetDivisions();
    }    [System.Web.Http.HttpGet]
    公共IEnumerable的<&区GT; GetCascadeDistrict(INT?师)
    {
        返回_db.GetCascadeDistrict(司);
    }    [System.Web.Http.HttpGet]
    公共IEnumerable的<&塔纳GT; GetCascadeUpzilla(INT?区)
    {
        返回_db.GetCascadeThana(区);
    }}


解决方案

您需要分离/通过的 <一个区分您的来电href=\"http://www.asp.net/web-api/overview/older-versions/creating-a-web-api-that-supports-crud-operations\"相对=nofollow> CRUD 操作,或 <一个href=\"http://www.asp.net/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2\"相对=nofollow>属性路由 取决于您使用您的项目中的的WebAPI 版本。

您不能使用相同的 CRUD HTTPGET 在没有把不同的路由属性相同的类/控制器的两倍。

您需要记住,在的WebAPI 方法在常规节目没有被称为他们的名字一样,因此的WebAPI 类/控制器不知道你的意思是从客户端调用(你的情况)的方法。

这就是为什么你需要:

的WebAPI 版本1:独立/由区分通话的 CRUD 操作。

的WebAPI 2版:独立/由区分通话的 属性路由

i have a angularJS controller where i put a kendo cascade dropdownlist.For dropdown list value ,on kendo dataSource read i am calling the web api service. For the first field the api GetDivisions() has been called and it response also but for the 2nd value the GetCascadeDistrict() method not called the GetDivisions() method called again. How can i solve this.Need Help

here's the angular Controller with kendo cascade dropdownlist.

app.controller("filterCtrl", function($scope, $sce,$http) {
var i;
$scope.dashImgSrc = $sce.trustAsResourceUrl('Content/Images/Bangladesh_Govt.gif');

$(document).ready(function () {

    var divisions = $("#divisions").kendoComboBox({
        filter: "contains",
        placeholder: "select a divisions...",
        dataTextField: "Name",
        dataValueField: "Id",
        animation: {
          close: {
              effects: "zoom:out",
              durations:250
          }  
        },
        dataSource: {
            type: "json",
            serverFiltering: true,
            transport: {
                read: "api/AreaService/GetDivisions()"
            }
        },
        change: function () {
            i = divisions.value();
            alert("1st hit"+i);
        }
    }).data("kendoComboBox");


    var districts = $("#districts").kendoComboBox({
        autoBind: false,
        cascadeFrom: "divisions",
        filter: "contains",
        placeholder: "select a district",
        dataTextField: "Name",
        dataValueField: "Id",
        dataSource: {
            type: "json",
            serverFiltering: true,
            transport: {
                read: function () {

                    alert("2nd hit");

                    //$http.get("/api/AreaService/GetCascadeDistrict(i)").success(function() {
                    //    alert("Hit the district api");
                    //}).error(function() {
                    //    alert("Error");
                    //});

                    $http({ method: "GET", url: 'api/AreaService/GetCascadeDistrict(i)' }).
                        success(function() {
                            alert("Actually it hit the custome get method");
                        }).
                        error(function() {
                            alert("Not hit or other problem");
                        });


                }
            }
        }


    }).data("kendoComboBox");


    var upazila = $("#upazila").kendoComboBox({
        autoBind: false,
        cascadeFrom: "districts",
        filter: "contains",
        placeholder: "select a  upazila...",
        dataTextField: "Name",
        dataValueField: "Id",
        dataSource: {
            type: "json",
            serverFiltering: true,
            transport: {
                read: function() {
                    $http.get("/api/AreaService/GetCascadeDistrict(i)").success(function() {

                    }).error(function() {

                    });
                }
            }
        }
    }).data("kendoComboBox");

    $("#get").click(function () {
        var divisionInfo = "\Division: { id: " + divisions.value() + ", name: " + divisions.text() + " }",
            districtInfo = "\nDistrict: { id: " + districts.value() + ", name: " + districts.text() + " }",
            upazilaInfo = "\nUpazila: { id: " + upazila.value() + ", name: " + upazila.text() + " }";

        alert("Road details:\n" + divisionInfo + districtInfo + upazilaInfo);
    });
});

});

And the Web api is here

public class AreaServiceController : ApiController
{
    private readonly AreaFilterManager _db = new AreaFilterManager();

    [System.Web.Http.HttpGet]
    public IEnumerable<Division> GetDivisions()
    {
        return _db.GetDivisions();
    }

    [System.Web.Http.HttpGet]
    public IEnumerable<District> GetCascadeDistrict(int? division)
    {
        return _db.GetCascadeDistrict(division);
    }

    [System.Web.Http.HttpGet]
    public IEnumerable<Thana> GetCascadeUpzilla(int? district)
    {
        return _db.GetCascadeThana(district);
    }

}

解决方案

You'll need to separate/distinguish your calls by CRUD operations or by Attribute Routing depends your WebApi version your using in your project.

You cannot use the same CRUD HttpGet twice in the same Class/Controller without putting a different routing attribute.

You need to remember, in WebApi the methods are not being called by their names like in regular programming, therefore the WebApi Class/Controller doesn't know which method you meant to call from your Client (in your case).

That's why you'll need to:

WebApi Ver 1 : separate/distinguish your calls by CRUD operations.

WebApi Ver 2 : separate/distinguish your calls by Attribute Routing.

这篇关于使用angularJS asp.net网站API调用自定义方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆