0

I am creating a new RESTful API which has an endpoint

GET v1/my-resource/{encoded-url}

When I try and send a message to this endpoint, I get a 404

GET v1/my-resource/%2Fshop%2Fgender-women-category-bags

When I try and send my-url it returns a 200.

GET v1/my-resource/my-url

This is my method:

[ApiController]
[Route("")]
public class GetController : ControllerBase
{
    [HttpGet("v1/my-resource/{url}")]
    public async Task<IActionResult> GetResource(
        [FromRoute, Required] string url)
    {
        return Ok();
    }
}

Can anyone help me understand why I can't pass %2Fshop%2Fgender-women-category-bags as the encoded url?

4
  • I wonder if the URL is being decoded by the framework. You could try [HttpGet("v1/my-resource/{**url}")]? Commented Mar 10, 2021 at 13:35
  • no luck :*( What does the ** do? Commented Mar 10, 2021 at 13:44
  • It's a catch-all route, basically everything after /my-resource/ would be included in the url variable. Note, I cannot replicate your problem in my code here. Commented Mar 10, 2021 at 13:50
  • checking this again, it does look like it worked, it just decoded the string, so I got /shop/gender-women-category-bags. Thanks for the help Commented Mar 10, 2021 at 14:23

1 Answer 1

1

You could try to base64 encode it so it produces a solid sequence and it doesn't confuse the ApiController. Btw. I've tried your solution in a mvc controller and it works, but not in api controller.

Sign up to request clarification or add additional context in comments.

1 Comment

its a good idea, but I don't have control over the route. This is already an agreed contract

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.