2016-11-16 75 views
0

我使用AngularJS控制器中的可空參數參數調用ASP.NET Web服務方法。如果我提供參數值,它工作正常,但如果沒有提供參數值,則不工作!並表示他以下錯誤:ASP.NET Web服務方法中缺少AngularJS可空參數

無法加載資源:

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

這裏是:服務器500(內部服務器錯誤)的狀態

並詳細回答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", 

     }) 

這裏是我的控制器:

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

    }) 

*任何幫助請!

+0

你想在每個請求中傳遞參數嗎? –

+0

Yeh !!但默認參數值應該爲空。 – TanvirArjel

回答

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; 
     }) 
    } 
+0

這不會發送空的Web服務它會發送「」參數,你可以這樣做,因爲我建議通過檢查未定義的避免代碼重複 –

+0

試過但沒有工作!奇特!!不知道爲什麼? – TanvirArjel

0

,而不是如果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; 
      }) 
     } 
+0

不按預期工作!像以前一樣工作! – TanvirArjel

+0

嘗試更新答案,並請檢查您的控制檯的錯誤可能是在$ routeParams.name的情況下undefined –

+0

沒有改變!現在甚至不能用於參數!是的,它是未定義的..現在該怎麼辦? – TanvirArjel