Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Sai Tej 4 posts 71 karma points
    Sep 01, 2022 @ 23:22
    Sai Tej
    0

    UmbracoApiController 404 Error .Net 5 Umbraco 9

    Hey,

    I am running .NET 5 and Umbraco 9.2.0 for my project. I am trying to create an API controller but I am only getting 404. The path I'm using is https://localhost:44349/umbraco/api/locations/getalllocations

    I made sure that the controller is being compiled as well. Any help would be appreciated.

    public class LocationsController : UmbracoApiController
        {
            private readonly ILogger<LocationsController> logger;
    
            private readonly LocationsAPIService locationsAPIService;
    
            public LocationsController(
                    ILogger<LocationsController> logger,
                    LocationsAPIService locationsAPIService
                )
            {
                this.logger = logger;
                this.locationsAPIService = new LocationsAPIService(new LocationAPISettingsFactory().Build());
            }
    
    
            public IEnumerable<string> GetAllLocations()
            {
                return new[] { "Table", "Chair", "Desk", "Computer" };
            }
    
        }
    
  • Lucas Bisgaard 19 posts 128 karma points c-trib
    Sep 02, 2022 @ 08:41
    Lucas Bisgaard
    0

    Hello, saitejkancharla.

    Just just stumple over the same isssue in umbraco 10. The solution was the return type

    So instead of

        public IEnumerable<string> GetAllLocations()
        {
            return new[] { "Table", "Chair", "Desk", "Computer" };
        }
    

    try it out where it is this

        public ActionResult<IEnumerable<string>> GetAllLocations()
        {
            return new[] { "Table", "Chair", "Desk", "Computer" };
        }
    
  • Sai Tej 4 posts 71 karma points
    Sep 02, 2022 @ 11:38
    Sai Tej
    0

    I tried returning ActionResult and IActionResult and both still return 404 error. I am not sure what I should be doing. I tried giving routes to the method and it still doesn't work.

      public ActionResult<IEnumerable<string>> GetAllLocations()
        {
            return new[] { "Table", "Chair", "Desk", "Computer" };
        } 
    
  • Huw Reddick 1932 posts 6722 karma points MVP 3x c-trib
    Sep 02, 2022 @ 12:38
    Huw Reddick
    0

    you don't need to use ActionResult

    public IEnumerable<string> GetAllLocations()
        {
            return new[] { "Table", "Chair", "Desk", "Computer" };
        }
    

    Just ran a quick test in my DEV system and it worked OK. Have you changed or added anything in startup.cs that may affect it?

  • Sai Tej 4 posts 71 karma points
    Sep 02, 2022 @ 20:16
    Sai Tej
    100

    I wasnt able to figure out why it didnt work but I deleted all my changes and redid them again and it started working. Go figure

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies