C
C#•2mo ago
Davaaron

Api Method Name Attribute

Hi, i just wonder what the name property inside the HttpMethod attribute is for? I read it can be used in MVC a lot when calling other controllers or getting their routes. So I have something like
[HttpDelete(Name = "DeleteLocation")]
public IActionResult Delete([FromQuery] int id)
{
return Ok(""Deleted"");
}
[HttpDelete(Name = "DeleteLocation")]
public IActionResult Delete([FromQuery] int id)
{
return Ok(""Deleted"");
}
Im usually used to something like [HttpDelete("{id}")]. I cannot specify both, right? I just create an api and want to be documentative/verbose about my code
6 Replies
Angius
Angius•2mo ago
You can specify both The name there is for the URL generator, for example. When you need a URL of this action, you can get it by the action name You can also get it by the controller name and action method name, sure But this tends to be easier
Davaaron
Davaaron•2mo ago
but that's only when I get the URL by the action in the code, right? it's not really changing the URL of my api? thanks for the quick response 🙂
Angius
Angius•2mo ago
ye Only things that influence the route are the [Route] and [Http<Method>] attributes
Davaaron
Davaaron•2mo ago
Alright, thanks! If i have a pure Api, I can leave it out then I guess But just curious, to specify both i would do [HttpDelete(Name = "DeleteLocation", RouteTemplate="{id}")]?
Angius
Angius•2mo ago
I think just [HttpDelete("{id}", Name = "DeleteLocation")] would work Could be mistaken, though If it doesn't, then yeah, what you say
Davaaron
Davaaron•2mo ago
alright, thanks
Want results from more Discord servers?
Add your server