Adding a letter at the end of an encoded URL causes 404

Viewed 37

This hits the corresponding method:

https://localhost:44379/annotation/getbytarget/https%3a%2f%2f

whereas this one causes 404:

https://localhost:44379/annotation/getbytarget/https%3a%2f%2fa

The only difference between the two URLs is that the latter has one extra letter (a) at the end. I am calling these URLs from the browser. But firstly, I had tried HttpClient's GetStringAsync method. No difference.

My goal is to call this URL:

https://localhost:44379/annotation/getbytarget/https://analyzer.app/analysis/60be725a980f947a351e2e97

I am encoding this URL so it becomes:

https://localhost:44379/annotation/getbytarget/https%3a%2f%2fanalyzer.app%2fanalysis%2f60be725a980f947a351e2e97

What could be the reason for this error?

This is an ASP.NET Core 3.1 Web API application and the corresponding Web API method is as follows:

[HttpGet("{id}")]
public ActionResult<List<Annotation>> GetByTarget(string id)
{
    var annotations = _annotationService.GetByTarget(id);

    if (annotations == null)
    {
        return NotFound();
    }

    return annotations;
}
1 Answers

I have solved the issue by changing the Web API method's routing signature (I am not sure if routing signature is the correct term). Notice that I have commented out the [HttpGet("{id}")] attribute:

//[HttpGet("{id}")]
public ActionResult<List<Annotation>> GetByTarget(string id)
{
    var annotations = _annotationService.GetByTarget(id);

    if (annotations == null)
    {
        return NotFound();
    }

    return annotations;
}

Now the method accepts calls this way (notice the id parameter):

https://localhost:44379/annotation/getbytarget?id=https%3a%2f%2fanalyzer.app%2fanalysis%2f
Related