ASP.NET Web Service方法中缺少AngularJS可空参数

时间:2016-11-16 06:03:59

标签: javascript angularjs web-services

我使用AngularJS控制器中的可为空的可选参数调用ASP.NET Web服务方法。如果我提供参数值,它可以正常工作但如果没有提供参数值则不起作用!!并显示他跟随错误:

无法加载资源:服务器响应状态为500(内部服务器错误)

和详细信息:

System.InvalidOperationException: Missing parameter: name.
   at System.Web.Services.Protocols.ValueCollectionParameterReader.Read(NameValueCollection collection)
   at System.Web.Services.Protocols.UrlParameterReader.Read(HttpRequest request)
   at System.Web.Services.Protocols.HttpServerProtocol.ReadParameters()
   at System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()

这是Web服务方法:

[WebMethod]
        public void GetAllStudents(string name)
        {
            IQueryable<Student> listStudents = dbContext.Students;
            if (!String.IsNullOrEmpty(name))
            {
                listStudents = listStudents.Where(x => x.name.Contains(name));
            }

            JavaScriptSerializer js = new JavaScriptSerializer();
            Context.Response.Write(js.Serialize(listStudents.ToList()));
        }

以下是我的路线配置:

  $routeProvider.when("/students/:name?",
        {
            templateUrl: "Templates/Students.html",
            controller: "studentsController",

        })

这是我的控制器:

.controller("studentsController", function ($scope, $http, $route, $location, $routeParams) {
        $scope.message = "Student Page";

        $scope.studentSearch = function () {
            if ($scope.name) {
                $location.url("/students/" + $scope.name);
            }
            else {
                $location.url("/students");
            }
        }

        if ($routeParams.name) {
            $http({
                method: "GET",
                url: "StudentService.asmx/GetAllStudents",
                params: { name: $routeParams.name }
            })
           .then(function (response) {
               $scope.students = response.data;
           })
        }
        else {
            $http.get("StudentService.asmx/GetAllStudents")
            .then(function (response) {
                $scope.students = response.data;
            })
        }

    })

*请帮助!!

2 个答案:

答案 0 :(得分:0)

而不是写if else语句只是在发送之前检查该param,如果没有值或者它是未定义的则赋值为null

 $routeParams.name = (!angular.isUndefined($routeParams.name) && $routeParams.name != "")?$routeParams.name:"";

  $http({
                method: "GET",
                url: "StudentService.asmx/GetAllStudents",
                params: { name: $routeParams.name }
            })
           .then(function (response) {
               $scope.students = response.data;
           })
        }

答案 1 :(得分:0)

它有效但看起来有些丑陋,连续两个if语句

if ($routeParams.name) {
                $http({
                    method: "GET",
                    url: "StudentService.asmx/GetAllStudents",
                    params: { name: $routeParams.name }
                })
               .then(function (response) {
                   $scope.students = response.data;
               })
            }

    if ($routeParams.name == undefined) {
        $http({
            method: "GET",
            url: "StudentService.asmx/GetAllStudents",
            params: { name: ""}
        })
       .then(function (response) {
           $scope.students = response.data;
       })
    }