C
C#9mo ago
Dharmang

How can I create an ardalis put endpoint request with id in route and payload in body?

Sample:
public class UpdateById : EndpointBaseAsync.WithRequest<DocumentTemplateUpdateByIdRequest>.WithActionResult<int>
{
private readonly IDocumentTemplateService _documentTemplateService;

public UpdateById(IDocumentTemplateService documentTemplateService)
{
_documentTemplateService = documentTemplateService;
}

[HttpPut("api/document-templates/{id}")]
[SwaggerOperation(
Description = "Update a Document Template by id",
OperationId = "DocumentTemplates.UpdateById",
Tags = new[] { "DocumentTemplate" }
)]
public override async Task<ActionResult<int>> HandleAsync([FromRoute] int id, [FromBody] DocumentTemplateUpdateByIdRequest request, CancellationToken cancellationToken = default)
{
var result = await _documentTemplateService.UpdateDocumentTemplateByIdAsync(request);
return result.ToActionResult(this);
}
}
public class UpdateById : EndpointBaseAsync.WithRequest<DocumentTemplateUpdateByIdRequest>.WithActionResult<int>
{
private readonly IDocumentTemplateService _documentTemplateService;

public UpdateById(IDocumentTemplateService documentTemplateService)
{
_documentTemplateService = documentTemplateService;
}

[HttpPut("api/document-templates/{id}")]
[SwaggerOperation(
Description = "Update a Document Template by id",
OperationId = "DocumentTemplates.UpdateById",
Tags = new[] { "DocumentTemplate" }
)]
public override async Task<ActionResult<int>> HandleAsync([FromRoute] int id, [FromBody] DocumentTemplateUpdateByIdRequest request, CancellationToken cancellationToken = default)
{
var result = await _documentTemplateService.UpdateDocumentTemplateByIdAsync(request);
return result.ToActionResult(this);
}
}
1 Reply
Dharmang
Dharmang9mo ago
Solved https://github.com/ardalis/apiendpoints#3-getting-started This might look simple but had wasted a few hours on this as I am new to C# and .NET in general but the repo has stated this on how to configure.
GitHub
GitHub - ardalis/ApiEndpoints: A project for supporting API Endpoin...
A project for supporting API Endpoints in ASP.NET Core web applications. - GitHub - ardalis/ApiEndpoints: A project for supporting API Endpoints in ASP.NET Core web applications.