ASP.NET Boilerplate WebApi 方法

4

我正试图在ASP.NET Boilerplate上启动我的项目。

我现在正在创建动态Web API,以下是我的代码:

我的AppService:

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方法的问题仍然存在。


看起来你需要确保你的路由定义正确。 - JK.
看起来他们是在执行不同的路由,这会导致一个错误,表示该路由未知。 - Tomasz
1
你能否发布你的控制器代码,以便调用请求的方法?你可能还需要包括路由。 - Dietz
我已经解决了请求不起作用的问题。这是POST中的“Content-Type”参数。我的工具将其设置为“text”,而应该是“application/json”。但是我仍然有关于POST/GET方法的问题。 - Tomasz
1个回答

2
默认的 HttpVerb 被设置为 Post。
/// <summary>
/// Default HTTP verb if not set.
/// </summary>
private const HttpVerb DefaultVerb = HttpVerb.Post;

然后在IApiControllerActionBuilder中有一种方法可以更改操作的动词。

IApiControllerActionBuilder<T> WithVerb(HttpVerb verb);

您��该能够通过以下调用更改动词(在 WebAPI 模块的 Initialize 中)
DynamicApiControllerBuilder
    .For<ITaskAppService>("tasksystem/taskService")
    .ForMethod("SomeMethod").WithVerb(HttpVerb.Get)
    .Build();

更多信息可以在网站上获取。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接