Copied to clipboard

Flag this post as spam?

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


  • pavan 5 posts 75 karma points
    Jan 18, 2024 @ 06:45
    pavan
    0

    Unable to Route the API Controller Method

    I had created a API named FormsApi and method name CreateLead here is the code.when i try to run the code its showing page not found 404 error.Here is the code

    [Microsoft.AspNetCore.Mvc.Route("FormsApi/[action]")] public class FormsApiController : UmbracoApiController {

    private readonly IPublishedContent _rootContent;
    
    public FormsApiController(IPublishedContent rootContent, IConfiguration configuration)
    {
        _rootContent = rootContent;
        this.configuration = configuration;
    }
    
    private readonly IConfiguration configuration;
    
    
    [Microsoft.AspNetCore.Mvc.HttpPost("CreateLead")]
    public async System.Threading.Tasks.Task<IHttpActionResult> CreateLead([System.Web.Http.FromBody] CRMLeadModel lead)
    {
    
    }
    

    }

  • Marc Goodson 2157 posts 14434 karma points MVP 9x c-trib
    Jan 18, 2024 @ 20:26
    Marc Goodson
    0

    Hi pavan

    something caught my eye in your code sample, might be wrong, but should it be

    Microsoft.AspNetCore.Mvc.FromBody

    https://github.com/dotnet/aspnetcore/blob/29a9c90edf6dbd773296723e8f82605d0364d9f2/src/Mvc/Mvc.Core/src/FromBodyAttribute.cs#L13

    rather than System.Web.Http.FromBody?

    regards

    Marc

  • Luuk Peters 85 posts 331 karma points
    Jan 23, 2024 @ 15:21
    Luuk Peters
    0

    If you want to use your own API's in an Umbraco site without doing custom routing, inherit from UmbracoApiController:

    https://docs.umbraco.com/umbraco-cms/reference/routing/umbraco-api-controllers.

    Also the 'FromBody' is not needed, because complex types are always read from the body. The 'FromBody' is only required if you want to force simple types (like string, guid etc) to be loaded from the body, because by default they are loaded from the querystring: https://learn.microsoft.com/en-us/aspnet/core/mvc/models/model-binding?view=aspnetcore-8.0#sources

  • 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