我需要使用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."}
更新 Web配置
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."}
答案 0 :(得分:1)
怎么样
$http.get(URL,{id:id})
这是将你的params作为get调用的第二个参数传递为map或string。
答案 1 :(得分:1)
您的id
未定义
PdfController
将您的配置路线更新为:
config.Routes.MapHttpRoute(
name:"PdfApi",
routeTemplate: "api/pdf/{id}",
defaults: new {controller = "PdfController", id=RouteParameter.Optional}
);
否则你可以使用不同的语法,即:
var id=1
$scope.EmailPdf = function () {
$http.get('/api/Pdf?id=id).success(function () {
$scope.PrintPreviewModal();
});
}
或
var _id=1;
var URL='/api/Pdf' ;
$scope.EmailPdf = function () {
$http.get(URL,{id:_id}).success(function () {
$scope.PrintPreviewModal();
});
}
更新
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."
其他选项
var id=1
$scope.EmailPdf = function () {
$http.get('/api/Pdf?id=id').success(function () {
$scope.PrintPreviewModal();
});
}
错误消息
{"$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."}
答案 2 :(得分:0)
我唯一需要的是
公共字符串Get(int id = 1)
我最初发布的代码不需要更改。谢谢