AngularJS中缺少的可空参数.. NET Web服务方法

AngularJS nullable parameter missing in ASP.NET Web Service method

本文关键字:NET 参数 Web 服务 方法 AngularJS      更新时间:2023-09-26

我正在调用ASP。从AngularJS控制器中带一个可选空参数的。NET Web Service方法。它的工作很好,如果我提供参数值,但不工作,如果参数值不提供!并显示以下错误:

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

和In details:

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()

Here is Web service

[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",
        })

Here is My Controller:

.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;
            })
        }
    })

*请帮忙!!

它可以工作,但看起来有点丑连续两个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;
       })
    }

不写if else语句,而是在发送前检查参数并在其中没有值或未定义时分配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;
           })
        }