Copied to clipboard

Flag this post as spam?

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


  • Nathan Woulfe 386 posts 1469 karma points MVP 3x c-trib
    Feb 07, 2017 @ 08:01
    Nathan Woulfe
    0

    Trying to set up a simple WebApi endpoint, using a custom route (ie not ~/umbraco/api/foo/bar) - figured it would be a matter of adding the WebApiConfig class, invoking it, and then using route attributes in my controller, inheriting from ApiController.

    I figured wrong - I get 404s on all routes. Wondering if there's any other Umbraco logic I need to be aware of, or if I'm just missing something obvious.

    I know code samples would help, but I'm at home and the code is at work...

  • Matt Brailsford 2474 posts 12077 karma points MVP 7x c-trib
    Feb 07, 2017 @ 11:01
    Matt Brailsford
    0

    Hey Nathan,

    Have you read through the Api Controller docs, it goes into pretty good detail as to how they should be setup in Umbraco

    https://our.umbraco.org/documentation/reference/routing/webapi/

    Hope this helps

    Matt

  • Nathan Woulfe 386 posts 1469 karma points MVP 3x c-trib
    Feb 07, 2017 @ 11:17
    Nathan Woulfe
    0

    Thanks Matt, read through that earlier. Should I be able to define my own routes against a controller inheriting from UmbracoApiController? It's not critical, but I'd prefer to define my own via attributes...

  • Aaron 21 posts 92 karma points
    Apr 05, 2017 @ 17:34
    Aaron
    1

    Had a similar problem getting web api working after umbraco was installed in a project.

    This blog post solved the problem for me, it appears you need to register your custom web api routing after umbraco has started and registered its routes.

    http://www.andreasjohansson.eu/technical-blog/configuring-custom-web-api-routing-for-umbraco-sites/

  • bronzewind 38 posts 121 karma points
    Aug 22, 2019 @ 15:44
    bronzewind
    2

    Hi everyone I was able to make this work but unfortunately this page is formatting it incorrectly so these are my pics of how I made it work in Umbraco V8:

    1. Created a composer: enter image description here

    2. Created a custom controller inheriting from UmbracoApiController: enter image description here

      1. Call it like GET http://[website_url]/frontend/api/customdata/{param1}/{param2}/
  • Sandra26 15 posts 115 karma points
    Oct 09, 2019 @ 13:00
    Sandra26
    0

    Hi bronzewind,

    I tried your solution. It was very easy to implement. But I'm getting 404 response. Any idea what can be the issue?

    Hope to hear from you, Sandra26

  • bronzewind 38 posts 121 karma points
    Oct 09, 2019 @ 13:26
    bronzewind
    0

    Hi Sandra,

    404 means the endpoint is not being found. please be sure you are calling the api with all the parameters, that you're inheriting from UmbracoApiController and the Composer matches the one in the example.

    If you don't find where the issue is, feel free of pasting here your code or more details about your implementation so that I can give it a look.

    Thanks!

  • Giovanni Sidoel 93 posts 230 karma points
    1 week ago
    Giovanni Sidoel
    0

    Hi boronzewind, I've implemented your solution, butnow i'm receiving an error about it not being able to create my controller:

    {
    "Message": "An error has occurred.",
    "ExceptionMessage": "An error occurred when trying to create a controller of type 'MyAssuriaController'. Make sure that the controller has a parameterless public constructor.",
    "ExceptionType": "System.InvalidOperationException",
    "StackTrace": "   at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)\r\n   at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__15.MoveNext()",
    "InnerException": {
        "Message": "An error has occurred.",
        "ExceptionMessage": "Object reference not set to an instance of an object.",
        "ExceptionType": "System.NullReferenceException",
        "StackTrace": "   at Umbraco.Web.Runtime.WebInitialComposer.<>c.<Compose>b__0_6(IFactory factory) in D:\\a\\1\\s\\src\\Umbraco.Web\\Runtime\\WebInitialComposer.cs:line 116\r\n   at Umbraco.Core.FactoryExtensions.GetInstance[T](IFactory factory) in D:\\a\\1\\s\\src\\Umbraco.Core\\FactoryExtensions.cs:line 22\r\n   at Umbraco.Web.WebApi.UmbracoApiControllerBase..ctor() in D:\\a\\1\\s\\src\\Umbraco.Web\\WebApi\\UmbracoApiControllerBase.cs:line 35\r\n   at MyAssuria.Controllers.MyAssuriaController..ctor() in C:\\inetpub\\wwwroot\\assuria\\App_Code\\MyAssuriaApi\\Controllers\\MyAssuriaController.cs:line 161\r\n   at DynamicMethod(Object[] )\r\n   at LightInject.PerRequestLifeTime.GetInstance(Func`1 createInstance, Scope scope) in C:\\projects\\lightinject\\src\\LightInject\\LightInject.cs:line 6207\r\n   at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.GetInstanceOrActivator(HttpRequestMessage request, Type controllerType, Func`1& activator)\r\n   at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)"
    }
    

    }

    I'm using Umbraco v8.5.5. I have also tried adding a parameterless constructor in the controller. This returns the exact same error.

    Anyd idea what might cause this issue and how to fix it?

  • bronzewind 38 posts 121 karma points
    1 week ago
    bronzewind
    0

    That is a different issue. Looks like you are using IoC. In that case, did you register your dependencies in a Composer?

    https://our.umbraco.com/documentation/reference/using-ioc/

  • Giovanni Sidoel 93 posts 230 karma points
    1 week ago
    Giovanni Sidoel
    0

    I'm not familiar with IoC, so if I am using IoC, it is not consciously. The thing is that this ApiController works fine if I leave out the composer for the custom routing. So I don't think I would need to do any sort of injection.

  • bronzewind 38 posts 121 karma points
    1 week ago
    bronzewind
    0

    Can you post your AsuriaController? And you composer?

  • Giovanni Sidoel 93 posts 230 karma points
    1 week ago
    Giovanni Sidoel
    0

    Composer

    using Ma.Controllers;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Http;
    using Umbraco.Core;
    using Umbraco.Core.Composing;
    
    namespace Ma.Composers
    {
        //Deze composer zorgt ervoor dat we custom routes kunnen gebruiken voor de api ipv de standaard ~/Umbraco/Api/[YourControllerName]
        //De routes zijn in de controller bij de methods aangegeven middels decorations [Route("custom/route")]
        [RuntimeLevel(MinLevel = RuntimeLevel.Run)]
        public class CustomApiRoutesComposer : ComponentComposer<CustomApiRoutesComponent>
        {
        }
    
        public class CustomApiRoutesComponent : IComponent
        {
    
            public void Initialize()
            {
                GlobalConfiguration.Configuration.MapHttpAttributeRoutes();
                GlobalConfiguration.Configuration.Initializer(GlobalConfiguration.Configuration);
            }
    
            public void Terminate()
            {
                throw new NotImplementedException();
            }
        }
    }
    

    Controller

    using Ma.Models;
    using System;
    using System.Dynamic;
    using System.Net;
    using System.Net.Http;
    using System.Web.Http;
    using Umbraco.Core.Models;
    using Umbraco.Core.Models.PublishedContent;
    using Umbraco.Core.Services;
    using Umbraco.Core.Services.Implement;
    using Umbraco.Web.WebApi;
    
    
    namespace Ma.Controllers
    {
        public class MyAssuriaController : UmbracoApiController
        {
    
            [HttpPost]
            [Route("customroute/myassuria/dologin")]
            public HttpResponseMessage Login(string username, string password) {
                ResponseData responseData = new ResponseData();
    
                if(Members.Login(username, password))
                {
                    IMemberService memberService = Services.MemberService;
    
                    IMember member = memberService.GetByUsername(username);
    
    
                    Profile profile = new Profile(member);
                    profile.AuthToken = Tools.GenerateAuthToken(member.Id, TOKEN_EXPIRE_DEFAULT);
    
                    ResponseObject response = new ResponseObject(profile);
    
                    responseData.Status = success;
                    responseData.Data = response;
    
                    return Request.CreateResponse(HttpStatusCode.OK, responseData);
                }
                else
                {
                    FailureData failure = new FailureData("username or password is missing", (int)HttpStatusCode.Forbidden);
    
                    responseData.Status = fail;
                    responseData.Data = failure;
    
                    return Request.CreateResponse(HttpStatusCode.Forbidden, responseData);
                }
            }
    }
    
  • bronzewind 38 posts 121 karma points
    1 week ago
    bronzewind
    0

    MemberService class? I don't see anything wrong in here, but can you expose that?

Please Sign in or register to post replies

Write your reply to:

Draft