【发布时间】:2014-09-21 05:54:20
【问题描述】:
我需要使用 id 作为标识符进行 get 调用。我只使用 Angular 资源完成了此操作,而不是 $http。我不了解 $http 如何与 Angular 一起工作。
角度控制器
$scope.EmailPdf = function () {
$http.get('/api/Pdf/{id}').success(function () {
$scope.PrintPreviewModal();
});
}
路由
config.Routes.MapHttpRoute(
name:"PdfApi",
routeTemplate: "api/{controller}/{id}",
defaults: new {controller = "apiGeneratePdf", id=RouteParameter.Optional}
);
api 控制器
public string Get(int id)
{
JobDataAdapter adapter = new JobDataAdapter();
Job job = new Job();
job = adapter.GetJob(id);
if (job == null)
{
return string.Empty;
}
try
{
错误信息
{"$id":"1","Message":"The request is invalid.","MessageDetail":"The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.String Get(Int32)' in 'TexasExterior.Controllers.PdfController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter."}
更新 网页配置
config.Routes.MapHttpRoute(
name: "PdfApi",
routeTemplate: "api/pdf/{id}",
defaults: new { controller = "PdfController", id = RouteParameter.Optional }
);
控制器
$scope.EmailPdf = function () {
$http.get('/api/Pdf/', { id: id }).success(function () {
$scope.PrintPreviewModal();
});
}
错误信息
ReferenceError: id 未定义
控制器
var id=1
$scope.EmailPdf = function () {
$http.get('/api/Pdf?id=id').success(function () {
$scope.PrintPreviewModal();
});
}
错误信息
{"$id":"1","Message":"请求无效。","MessageDetail":"参数字典包含不可为空类型'System.'的参数'id'的空条目。 'TexasExterior.Controllers.PdfController' 中方法 'System.String Get(Int32)' 的 Int32'。可选参数必须是引用类型、可空类型或声明为可选参数。"}
控制器
var _id = 1;
var URL = '/api/Pdf';
$scope.EmailPdf = function () {
$http.get(URL, { id: _id }).success(function () {
$scope.PrintPreviewModal();
});
}
错误信息
{"$id":"1","Message":"No HTTP resource was found that matches the request URI 'http://localhost:44301/api/Pdf'.","MessageDetail":"No action was found on the controller 'Pdf' that matches the request."}
【问题讨论】:
-
您不会在 javascript 代码中使用模板语法 {id}。大概 id 在脚本本地可用,在这种情况下,您只需要类似 "$http.get('/api/Pdf/'+id).success(function () {"
-
错误信息非常具有描述性,你没有为 id 传递任何东西,试试这个:
$http.get('/api/Pdf/'+id) -
HTTP 错误 404.11 - 未找到 请求过滤模块被配置为拒绝包含双转义序列的请求。
-
看起来像一个安全问题?
标签: ajax asp.net-mvc angularjs asp.net-web-api