C
C#•4mo 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•4mo 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
DavaaronOP•4mo 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•4mo ago
ye Only things that influence the route are the [Route] and [Http<Method>] attributes
Davaaron
DavaaronOP•4mo 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•4mo ago
I think just [HttpDelete("{id}", Name = "DeleteLocation")] would work Could be mistaken, though If it doesn't, then yeah, what you say
Davaaron
DavaaronOP•4mo ago
alright, thanks
Want results from more Discord servers?
Add your server