Reputation: 400
In my startup.cs class I have:
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
My controller currently looks like so:
[Microsoft.AspNetCore.Components.Route("api/my-test")]
public class MyTestController : ControllerBase
{
[HttpGet]
[Route("{confirmationCode}")]
public async Task<IActionResult> Get(string confirmationCode)
{
return Ok($"The confirmation code is: {confirmationCode}");
}
}
How can I call the Get action on my MyTestController and get access to the confirmation code that was passed in as part of the URL from a route URL that looks like this: /api/my-test/abc123
Upvotes: 0
Views: 337
Reputation: 400
Figured out the solution on my own after a few more attempts:
[Route("api/my-test")] // <-- this Route is from Microsoft.AspNetCore.Mvc.RouteAttribute
public class MyTestController : ControllerBase
{
// GET api/my-test/abc123
[HttpGet("{confirmationCode}")] // <-- instead of Route, you tack on the rest of your route pattern into the HttpGet attribute (or other verbs depending on your code)
public async Task<IActionResult> Get(string confirmationCode)
{
return Ok($"The confirmation code is: {confirmationCode}");
}
}
Here are the two changes / differences:
Upvotes: 2