【问题标题】:ASP.NET Boilerplate WebApi methodsASP.NET 样板 WebApi 方法
【发布时间】:2015-01-13 22:13:56
【问题描述】:

我正在尝试在 ASP.NET Boilerplate 上开展我的项目。

我现在正在创建动态 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 方法(样板文档中没有相关信息)

当我在没有任何数据的情况下调用方法 [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 方法的问题仍然悬而未决。

【问题讨论】:

  • 看起来您需要确保您的路线定义正确。
  • 似乎是。执行不同的路由会给出一个未知路由的错误
  • 您能否发布您希望通过请求调用的方法的控制器代码?您可能还应该包括路由。
  • 我已经处理了请求不起作用的问题。它是 POST 中的“Content-Type”参数。我的工具将其设置为“文本”,应该是“应用程序/json”。但我对 POST/GET 方法仍有疑问

标签: c# asp.net asp.net-web-api boilerplate


【解决方案1】:

默认的 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();

更多信息可咨询on the website

【讨论】:

    猜你喜欢
    • 2021-12-14
    • 2015-05-30
    • 2016-05-06
    • 1970-01-01
    • 2018-08-10
    • 2014-07-02
    • 1970-01-01
    • 2013-03-26
    • 2019-09-22
    相关资源
    最近更新 更多