What is the purpose of the Name parameter in HttpPostAttribute
Asked Answered
A

3

6

I am seeing the following code being applied in .net core action methods:

[HttpPost("MyAction", Name = "MyAction")]
public IActionResult MyAction()
{
    // some code here
}

What is the purpose of the "Name" parameter in the HttpPost attribute?

Ally answered 22/10, 2019 at 5:50 Comment(1)
Thank you very much for the reply and details. Does that mean that I can access the end point with both the URLs: servername.com/ControllerName/ActionName as well as servername.com/ControllerName/NameAttributeValue ?Ally
S
9

The Name property is used for Url Generation. It has nothing to do with routing! You can omit it almost all the time.

Add the following code to your controller and you will get the "Aha!":

[HttpGet("qqq", Name = "xxx")]
public string yyy()
{
   return "This is the action yyy";
}

[HttpGet("test")]
public string test()
{
    var url = Url.Link("xxx", null);  //Mine is https://localhost:44384/api/qqq
    return $"The url of Route Name xxx is {url}";
}

The Name property in the first action, when used, for example, to generate a url, is merely used to reference the action yyy. In my set up, invoking /api/test returns the string The url of Route Name xxx is https://localhost:44384/api/qqq.

Action yyy is reachable by the route .../qqq, the first parameter passed to the HttpGet attribute constructor.

Skinflint answered 31/10, 2019 at 6:18 Comment(0)
R
4

From the source

    /// <summary>
    /// Gets the route name. The route name can be used to generate a link using a specific route, instead
    ///  of relying on selection of a route based on the given set of route values.
    /// </summary>
    string Name { get; }

Example usage; If you have two methods with the same name that take different parameters, you can use Name parameter to differentiate Action Names.

Regretful answered 22/10, 2019 at 5:59 Comment(1)
And how to call them why no url example?Magna
H
4

From document :

Route names can be used to generate a URL based on a specific route. Route names have no impact on the URL matching behavior of routing and are only used for URL generation. Route names must be unique application-wide.

It can be used to generate a URL based on a specific route . For example , route defines like :

[HttpGet("{id}", Name = "GetContact")]
public IActionResult GetById(string id)
{
    var contact = contactRepository.Get(id);
    if (contact == null)
    {
        return NotFound();
    }
    return new ObjectResult(contact);
}

You can use CreatedAtRoute method to return with the content of newly contact as well as the URI of it. The CreatedAtRoute method will based on the route name "GetContact" and id to generate the URI:

[HttpPost]
public IActionResult Create([FromBody] Contact contact)
{
    if (contact == null)
    {
        return BadRequest();
    }
    contactRepository.Add(contact);
    return CreatedAtRoute("GetContact", new { id = contact.ContactId }, contact);
}
Hydrothermal answered 22/10, 2019 at 6:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.