Hi I'm trying to create an UmbracoApiController and from what I've read the default route should be umbraco/api/{controller}/{action}/{id}
My code is from a project and gets copied into the sites/bin folder.
I have the following
namespace My.Controllers
{
using System.Net;
using System.Net.Http;
using Umbraco.Web.WebApi;
publicclassTestApiController : UmbracoApiController
{
publicHttpResponseMessage Index() {
var response = this.Request.CreateResponse(HttpStatusCode.OK);
response.Content = newStringContent("why is this not working??");
return response;
}
}
}
I'm not sure if this will make a difference, but I think since your method is named Index, you might not need the method name on the URL, so https://mysite.com/umbraco/api/testapi/
Also, I'd try decorating the method with the [RequireHttps] attribute?
I noticed your URL is https which I'm not sure if that has something to do with your issue
Nope no joy moving the using references to before the namespace, having the usings within the namespace works fine for any suface controllers I have within the project.
public HttpResponseMessage Index(int? id) {
var response = this.Request.CreateResponse(HttpStatusCode.OK);
response.Content = new StringContent("why is this not working??");
return response;
}
nope, I have a breakpoint on my action and its not even hitting the method. Must be the routing, but just can't see why?
It is OK to use the UmbracoApiController in the same project as my main project isn't it? i.e. my main project is all my code behind for mysite (Models, Controllers (SurfaceControllers, etc) this project gets built then copied into the httpdocs/bin of my Umbraco site
UmbracoApiController - default route??
Hi I'm trying to create an UmbracoApiController and from what I've read the default route should be umbraco/api/{controller}/{action}/{id}
My code is from a project and gets copied into the sites/bin folder.
I have the following
But when I try a request from https://mysite.com/umbraco/api/testapi/index
The page displays the following
The default routing for Surface controllers works fine from the same project?
Any ideas where I'm going wrong??
Thanks
Hi Graeme,
I'm not sure if this will make a difference, but I think since your method is named Index, you might not need the method name on the URL, so https://mysite.com/umbraco/api/testapi/
Also, I'd try decorating the method with the
[RequireHttps]
attribute?I noticed your URL is https which I'm not sure if that has something to do with your issue
Richard
Hi Richard,
Without the action name (Index) I get a 404, I've also decorated the action with [RequireHttps] attribute but I still get the same result
Hi Graeme,
My only other suggestion is that perhaps having the using statements inside your namespace may be causing compilation issues?
Try moving them to before the namespace. See this article: http://www.hanselman.com/blog/BackToBasicsDoNamespaceUsingDirectivesAffectAssemblyLoading.aspx
Nope no joy moving the using references to before the namespace, having the usings within the namespace works fine for any suface controllers I have within the project.
Thanks anyway.
Actually,
I've read some more on that and it won't make any difference at all.
It's more likely related to the routing.
take a look in the App_Start folder and in the WebApiConfig file - is the default route expecting an id?
If so, I'd add a new route ABOVE this default one, without any parameter.
Richard
I've also tried adding my own custom route
request : https://mysite.com/mytestapi/testapi/index
still gets the error message
try:
nope, I have a breakpoint on my action and its not even hitting the method. Must be the routing, but just can't see why?
It is OK to use the UmbracoApiController in the same project as my main project isn't it? i.e. my main project is all my code behind for mysite (Models, Controllers (SurfaceControllers, etc) this project gets built then copied into the httpdocs/bin of my Umbraco site
Yeah that's fine, umbraco api controller inherits from apicontroller in .net anyway.
I think you're right about the route. Try:
);
Nope still no luck, still getting the error when trying the custom route without the action
No HTTP resource was found that matches the request URI 'https://mysite.com/mytestapi/testapi/'
No type was found that matches the controller named 'testapi'
The URL should definitely be https://mysite.com/umbraco/api/testapi/index/
It really sounds like the routing is the issue but I'm a bit lost as to what current state things are in.
I think you the default webapi route should be fine as Umbraco web api inherits from standard .net api
Can you post the current code for the route and for the method so we can start over?
Hi Richard,
Just tried the default umbraco route again umbraco/api/testapi/index/ and it returned a different error
The requested resource does not support http method 'GET'.
It must have been since I'd added the nullable int param, since adding that I think I'd only been trying my custom route.
I resolved the above error by decorating the action with [System.Web.Http.AcceptVerbs("GET")]
need the FQN as AcceptVerbs clashed with the System.Web.Mvc reference.
Thanks for your help and input.
Graeme
Glad you got it sorted :)
is working on a reply...