Umbraco Api Controller - parameter from FromRoute
# help-with-umbraco
u
Hi everyone! I'm struggling with UmbracoApiController and route parameters. I can't seem to find information on how to retrieve a parameter from the route. I've attempted various approaches, but something always seems to break. Moreover, if I introduce a custom route, I end up losing the default "umbraco/api" prefix. Here's the code I've been working with:
Copy code
public sealed class ApplicationSubmitController : UmbracoApiController
{
    private readonly IApplicationSubmitService _applicationAsSubmittedService;

    public ApplicationSubmitController(IApplicationSubmitService applicationAsSubmittedService)
    {
        _applicationAsSubmittedService = applicationAsSubmittedService;
    }

    // expected: /umbraco/api/applicationSubmit/submited/{applicationId}
    [HttpGet]
    public IActionResult Submited([FromRoute] int applicationId)
    {
        return Ok(new { applicationId });
    }
}
Would appreciate any guidance or pointers. Thanks in advance!
m
Does this help (https://docs.umbraco.com/umbraco-cms/reference/routing/umbraco-api-controllers#using-mvc-attribute-routing-in-umbraco-web-api-controllers) I presume if you want to keep the
umbraco/api/...
then you just specify that in your route?
Copy code
csharp
public class ProductsController : UmbracoApiController
{
    public IEnumerable<string> GetAllProducts()
    {
        return new[] {"Table", "Chair", "Desk", "Computer"};
    }

    [Route("product/{id?}")]
    public string GetProduct(int? id)
    {
        if (id is not null)
        {
            return $"Monitor model {id}";
        }
        return "Base model Monitor";
    }
}
according to https://learn.microsoft.com/en-us/aspnet/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2#route-prefixes you can also type that id`[Route("product/{id:int}")]` if nullable isn't what you are after. Also you can use Route like routePrefix from .net framework
Copy code
csharp
[Route("api/[controller]/[action]")]
public class DistrictController : ControllerBase
{

    [Route("{id:int:min(1)}")] // i.e. GET /api/District/GetDetails/10
    public IActionResult GetDetails(int id)
    {
    }

    // i.e. GET /api/District/GetPage/?id=10
    public IActionResult GetPage(int page)
    {
    }

    [HttpDelete]
    [Route("{id:int:min(1)}")] // i.e. Delete /api/District/Delete/10
    public IActionResult Delete(int id)
    {
    }

    [HttpGet]
    [Route("~/api/States/GetAllState")] // i.e. GET /api/States/GetAllState
    public IActionResult GetStates()
    {
    }
}
u
Thanks for your answer! 🙂 I end up doing sth like this
Copy code
[HttpGet("/umbraco/api/applicationSubmit/submited/{applicationId}")]
public async Task<IActionResult> Submited([FromRoute] int applicationId)
{
    var isSubmitted = await _applicationAsSubmittedService.IsApplicationSubmittedToCrmAsync(applicationId);
    return Ok(new { Submitted = isSubmitted });
}
is there any better way?
I'm just wondering why I cant use sth like this? 🙈
Copy code
[HttpGet("submited/{applicationId}")]
public async Task<IActionResult> Submited([FromRoute] int applicationId)
m
I'd not come across route templates in httpGet.. but https://learn.microsoft.com/en-us/aspnet/core/mvc/controllers/routing?view=aspnetcore-7.0#attribute-routing-with-http-verb-attributes
Copy code
csharp
[Route("api/[controller]")]
[ApiController]
public class Test2Controller : ControllerBase
{
    [HttpGet]   // GET /api/test2
    public IActionResult ListProducts()
    {
        return ControllerContext.MyDisplayRouteInfo();
    }

    [HttpGet("{id}")]   // GET /api/test2/xyz
    public IActionResult GetProduct(string id)
    {
       return ControllerContext.MyDisplayRouteInfo(id);
    }

    [HttpGet("int/{id:int}")] // GET /api/test2/int/3
    public IActionResult GetIntProduct(int id)
    {
        return ControllerContext.MyDisplayRouteInfo(id);
    }

    [HttpGet("int2/{id}")]  // GET /api/test2/int2/3
    public IActionResult GetInt2Product(int id)
    {
        return ControllerContext.MyDisplayRouteInfo(id);
    }
}
would seem to imply that you last code sippet should be routed at
/submitted/1
and do you even need the
[FromRoute]
? if you don't have the any route attribute on the class?
fyi diplo godmode > diagnostics > mvc configuration might help as it lists the routes it believes you have registered...
3 Views