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...
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...
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.
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:
Created a composer:
Created a custom controller inheriting from UmbracoApiController:
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.
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?
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.
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);
}
}
}
WebApi routing help
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...
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
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...
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/
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:
Created a composer:
Created a custom controller inheriting from UmbracoApiController:
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
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!
Hi boronzewind, I've implemented your solution, butnow i'm receiving an error about it not being able to create my controller:
}
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?
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/
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.
Can you post your AsuriaController? And you composer?
Composer
Controller
MemberService class? I don't see anything wrong in here, but can you expose that?
is working on a reply...