1

When creating a new ASP.net WebAPi Rest controller, visual studio adds a DELETE action like this

// DELETE: api/foo/5
public void Delete(int id)
{
}

And I can successfully call that endpoint

But the 'id' in my system are string hashes so I need to pass the 'id' as a string not an int e.g

// DELETE: api/foo/58c75babbf61cda99c84af8b
public void Delete(string id)
{
}

But when I change this, the DELETE action no longer is called and I get the error "No HTTP resource was found that matches the request URI "

Any ideas why? and how do get around this?

1
  • this is a route config issue. show how you configured routes. provide minimal reproducible example that can be used to reproduce the problem. Commented Mar 20, 2017 at 22:58

1 Answer 1

5

You can specify the route template yourself, and in it, specify that id is a guid:

[HttpDelete, Route("api/foo/{id}")]
public void Delete(string id)
{
}
Sign up to request clarification or add additional context in comments.

4 Comments

But the Id is not a guid. It's just a string.
OK, I see. Sorry for the misunderstanding, see my edited answer.
Your edit doesn't work either, but what does work is if the attribute looks like this [HttpDelete, Route("api/foo/{id}")]
I checked the documentation. Unfortunately alpha really means 'alpha' and not 'alphanumberic', so numbers are out... But I guess we could still use the regex route constraint like this {id:regex(^[a-zA-Z0-9]+$)}. This should match any number of alphanumeric character. But if it works just by specifying the type, I guess that's the best solution :) Thanks for the educational question and sharing the answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.