AngularJS中缺少的可空参数.. NET Web服务方法
AngularJS nullable parameter missing in ASP.NET Web Service method
我正在调用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;
})
}
相关文章:
- 使用jquery变量作为.net MVC中ActionLink的参数
- 如何将javascript变量作为参数提供给asp.net方法
- 使用评估函数调用 javascript 函数作为文本框 Asp.net 参数
- HTTP GET中的C#MVC ASP.NET URL参数未正确编码
- 如何使用asp.net将gridview的行号作为命令参数传递
- Asp.net向javascript函数发布了多个参数
- 使用来自 asp.net 的参数调用 Java 脚本函数
- 如何使用一些变量参数从javascript函数调用 asp.net C#函数并从C#函数获取字符串返回
- 当 JSON.stringify 时,ASP.NET MVC 控制器参数的 JSON 为 null
- 如何将一些参数从javascript接收响应字符串 asp.net 页面并更新html文档
- Ajaxupload.js如何将查询参数传递给ashx文件 asp.net
- C# asp.net MVC - 使用参数从 html.actionlink 调用脚本函数
- AngularJS $http.post ASP.NET 具有简单类型参数的 Web API 控制器
- 如何在 asp.net 中使用 C# 代码将参数传递给 JavaScript 函数
- 将 JavaScript 参数传递给 asp.net
- 如何从控制器 ASP.NET url 中删除哈希参数,以便返回到 Angular.JS 应用程序中的根 URI
- ASP.NET-参数的类型是什么.价值
- 未捕获的语法错误:) 在 MVC 应用程序 JavaScript asp.net 参数列表后丢失
- 如何从JavaScript传递值到ASP.NET c#参数
- 如何管理ASP.. NET参数和Javascript键