Home > OS >  Why is a Guid parameter arriving as default in ASP method?
Why is a Guid parameter arriving as default in ASP method?

Time:01-06

I have the following code:

    [HttpGet]
    [Route("{id}")]
    public Task<ActionResult<Model?>> Get(
        Guid id)
    {
        return ReadModel(id);
    }

In the watch, if I inspect Request.Path I can see that the path is /api/Model/798f2724-c01b-4836-9603-5b64acaed464, but when I inspect id it has a value of 00000000-0000-0000-0000-000000000000

I'm calling this from postman with the following URL https://localhost:7685/api/Model/798f2724-c01b-4836-9603-5b64acaed464

I've also tried adding this in the method just to see if something was off about the Guid, but this object is created correctly:

Guid temp = new Guid("798f2724-c01b-4836-9603-5b64acaed464");

Am I supposed to do something to tell asp how to parse a guid?

CodePudding user response:

It should be working properly,recheck id if it is valid one more time. I would add an type validation to the action attribute and add async since you are using a task

        [HttpGet("{id:Guid}")]
        public async Task<ActionResult<Model?>> Get(Guid id)

or if you don't have any async code inside of the action

        [HttpGet("{id:Guid}")]
        public ActionResult<Model?> Get(Guid id)
  • Related