Git Product home page Git Product logo

Comments (8)

maxkoshevoi avatar maxkoshevoi commented on August 16, 2024 1

Answer 2:

Doesn't this section of readme addresses this exact question?
https://github.com/ardalis/ApiEndpoints#how-can-i-bind-parameters-from-multiple-locations-to-my-model

from apiendpoints.

maxkoshevoi avatar maxkoshevoi commented on August 16, 2024

Try

[HttpPut("/investment-types/{Id}")]
public override async Task<ActionResult<InvestmentTypePutResponse>> HandleAsync(Guid id, InvestmentTypePutRequest request, CancellationToken cancellationToken = default) {}

public class InvestmentTypePutRequest
{
  public byte[] Version { get; set; } = null!;
  public string Name { get; set; } = null!;
  public string? Description { get; set; }
}

Edit: Oh, right, that cannot be done since HandleAsync signature is fixed..

from apiendpoints.

maxkoshevoi avatar maxkoshevoi commented on August 16, 2024

Okay, let's think about it in a different way.
Why does Handle method needs to be predefined at all? We can just do this:

public class Put : BaseEndpointAsync
{
    [HttpPut("/investment-types/{Id:Guid}")]
    public async Task<ActionResult<InvestmentTypePutResponse>> HandleAsync(Guid id, InvestmentTypePutRequest request)
	{

    }
}

public class InvestmentTypePutRequest
{
  public byte[] Version { get; set; } = null!;
  public string Name { get; set; } = null!;
  public string? Description { get; set; }
}

And analyzer will keep us from defining multiple actions.

Side note:

  • I think it's ok to define all actions this way. No WithRequest/WithResponce needed. MediatR needs them to resolve information about the command/query so it can be called from some other code, but in this case Asp.Net will resolve all information, and direct invocations to our action automatically.
  • So essentially, this package can be boiled down to
    • Base class BaseEndpoint
    • Analyzer (that's keeps developers from creating more than one action in a controller)
    • And maybe source generator (that will automatically add [Route] attribute to every controller based on it's folder or some other criteria). Everything else (file nesting and request handling) is already built-in. maxkoshevoi@41e49ee

from apiendpoints.

ardalis avatar ardalis commented on August 16, 2024

Original question seemed to indicate that wasn't working for him, but admittedly I don't have a reproduction of the problem, yet.

from apiendpoints.

maxkoshevoi avatar maxkoshevoi commented on August 16, 2024

Original question seemed to indicate that wasn't working for him

Maybe it's because [FromRoute] is missing from parameter's signature?

image

I haven't tried to reproduce it either, but I can say for sure that inheriting directly from BaseEndpointAsync would work.

from apiendpoints.

 avatar commented on August 16, 2024

This issue was created by Steve for me. I've created a VS solution with a repro at https://github.com/msantor/ApiEnpointsIssue124. I've included a Postman collection with the test call I'm using. It's possible I'm missing something very simple. Thank you for your time.

from apiendpoints.

 avatar commented on August 16, 2024

I resolved my problem. It was because I had multiple [FromBody] attributes directly on the request type. I created an additional class to represent the body of the request and model binding does the rest. Sorry to have wasted everyone's time on this. I needed to more thoroughly read through the documentation. Please close this issue.

from apiendpoints.

ardalis avatar ardalis commented on August 16, 2024

Ah, yes. That will do it. We've all been there. :)

from apiendpoints.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.