我正在尝试在 ASP.NET 样板中开始我的项目。
我现在正在创建动态 Web API,这是我所拥有的:
我的应用服务:
public interface IBorrowLendAppService : IApplicationService
{
GetBorrowLendsOutput GetBorrowLends(GetBorrowLendsInput input);
}
我的输入:
public class GetBorrowLendsInput : IInputDto
{
public byte ItemType { get; set; }
public int PersonId { get; set; }
}
这是我的问题:
- 如何调用这个方法?
- 如何创建 GET/POST 方法(Boilerplate 文档中没有相关信息)
当我在没有任何数据的情况下调用方法 [GET]GetBorrowLends 时,我收到错误:
{
"result": null,
"success": false,
"error": {
"code": 0,
"message": "Your request is not valid!",
"details": null,
"validationErrors": [
{
"message": "input is null!",
"members": [
"input"
]
}
]
},
"unAuthorizedRequest": false
}
当我尝试像这样调用它时:
.../GetBorrowLends?ItemType=0&PersonId=1
我收到同样的错误
使用数据调用 [POST]:
{
"input":{
"ItemType":"0",
"PersonId":"1"
}
}
返回另一个错误:
{
"result": null,
"success": false,
"error": {
"code": 0,
"message": "An internal error occured during your request!",
"details": null,
"validationErrors": null
},
"unAuthorizedRequest": false
}
我该如何处理?以及如何手动创建 GET/POST 方法?
谢谢
编辑:
我已经处理了端点不工作的问题。我在 POST 消息中错误地设置了“Content-Type”参数。
但是有关 ApplicationService 中的 GET/POST 方法的问题仍然悬而未决。
最佳答案
默认的 HttpVerb 设置为 Post。
/// <summary>
/// Default HTTP verb if not set.
/// </summary>
private const HttpVerb DefaultVerb = HttpVerb.Post;
然后在IApiControllerActionBuilder
中有一个方法来改变 Action 的动词。
IApiControllerActionBuilder<T> WithVerb(HttpVerb verb);
您应该能够通过以下调用更改动词(在 WebAPI 模块的 Initialize
中)
DynamicApiControllerBuilder
.For<ITaskAppService>("tasksystem/taskService")
.ForMethod("SomeMethod").WithVerb(HttpVerb.Get)
.Build();
可获得更多信息on the website .
关于c# - ASP.NET 样板 WebApi 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27932469/