gpt4 book ai didi

c# - AngularJS $resource 在 Web Api 中搜索项目的工作示例

转载 作者:太空狗 更新时间:2023-10-29 21:17:31 25 4
gpt4 key购买 nike

我正在学习如何使用 AngularJS 的 $resource 调用 Web Api 后端。我想将对象层次结构作为标准传递并取回 IEnumerable<Program> .以下是标准示例:

$scope.criteria = {
Categories:[
{
Name: "Cat1",
Options: [
{Text: "Opt1", Value: true},
{Text: "Opt2", Value: false}
]
},
{
Name: "Cat2",
Options: [
{Text: "Opt3", Value: true},
{Text: "Opt4", Value: false}
]
}
]
}

我在服务器上用 C# 定义了相同的对象。

public class CriteriaModel
{
public IEnumerable<CriteriaCategory> Categories { get; set; }
}

public class CriteriaCategory
{
public string Name { get; set; }
public IEnumerable<CriteriaOption> Options { get; set; }
}

public class CriteriaOption
{
public string Text { get; set; }
public bool Value { get; set; }
}

这是我配置 $resource 的方式:

angular.module('my.services')
.factory('api', [
'$resource',
function ($resource) {
return {
Profile: $resource('/api/profile/:id', { id: '@id' }),
Settings: $resource('/api/settings/:id', { id: '@id' }),
Program: $resource('/api/program/:id', { id: '@id' })
};
}
]);

我这样调用它:

api.Program.query({ criteria: $scope.criteria }, function (response) {
$scope.programs = response;
});

无论我尝试什么,我要么得到 null作为条件参数或操作根本不执行。我不知道问题出在 Angular 、网络 API 还是两者都有。这是操作:

public class ProgramController : ApiController
{
public IEnumerable<Program> GetByCriteria([FromUri]CriteriaModel criteria)
{
// Either criteria is null or this action doesn't even get
// executed depending on what I try.
}
}

谁能帮我找到一个使用 AngularJS $resource 和 Web Api 搜索和返回项目的工作示例?

最佳答案

您将需要一个自定义模型 Binder 。来自 what I understand FromUri 不会处理 $resource 将放入查询字符串中的复杂嵌套类型或 json。

模型 Binder :

public class CriteriaModelBinder : IModelBinder
{
public bool BindModel(
HttpActionContext actionContext,
ModelBindingContext bindingContext
)
{
if (bindingContext.ModelType != typeof (CriteriaModel))
{
return false;
}

var value = bindingContext.ValueProvider.GetValue("Categories");

if (value == null)
{
return false;
}

var categoryJson = value.RawValue as IEnumerable<string>;

if (categoryJson == null)
{
bindingContext.ModelState.AddModelError(
bindingContext.ModelName, "Categories cannot be null.");
return false;
}

var categories = categoryJson
.Select(JsonConvert.DeserializeObject<CriteriaCategory>)
.ToList();

bindingContext.Model = new CriteriaModel {Categories = categories};
return true;
}
}

Controller :

[RoutePrefix("api/program")]
public class ProgramController : ApiController
{
[Route("getbycriteria")]
[HttpGet]
public HttpResponseMessage GetByCriteria(
[ModelBinder(typeof(CriteriaModelBinder))]CriteriaModel criteria
)
{
return new HttpResponseMessage(HttpStatusCode.OK);
}
}

Angular Controller :

angular.module('myApp').
controller('HomeController', function($scope, $resource) {
var Program = $resource('/api/program/:id', {}, {
getByCriteria: {
url: '/api/program/getbycriteria',
method: 'GET',
isArray: true
}
});

var program = new Program();
var criteria = {
Categories: [
{
Name: "Cat1",
Options: [
{ Text: "Opt1", Value: true },
{ Text: "Opt2", Value: false }
]
},
{
Name: "Cat2",
Options: [
{ Text: "Opt3", Value: true },
{ Text: "Opt4", Value: false }
]
}
]
};

$scope.submit = function () {
console.log(program);
program.$getByCriteria(criteria);
};
});

编辑:

这里是POST:

Controller :

[RoutePrefix("api/program")]
public class ProgramController : ApiController
{
[Route("getbycriteria")]
[HttpPost]
public HttpResponseMessage GetByCriteria(CriteriaModel criteria)
{
return new HttpResponseMessage(HttpStatusCode.OK);
}
}

Angular :

angular.module('myApp').
controller('HomeController', function($scope, $resource) {
var Program = $resource('/api/program/:id', {}, {
getByCriteria: {
url: '/api/program/getbycriteria',
method: 'POST',
isArray: true
}
});

var program = new Program();
program.Categories = [
{
Name: "Cat1",
Options: [
{ Text: "Opt1", Value: true },
{ Text: "Opt2", Value: false }
]
},
{
Name: "Cat2",
Options: [
{ Text: "Opt3", Value: true },
{ Text: "Opt4", Value: false }
]
}
];

$scope.submit = function () {
console.log(program);
program.$getByCriteria();
};
});

关于c# - AngularJS $resource 在 Web Api 中搜索项目的工作示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23063949/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com