How to use ASP.NET Web API in Umbraco 4 & .NET 4.0
So, you really like the idea of the ASP.NET Web API and can't wait to use it? But you're using Umbraco 4 and / or Webforms? Good news everybody! It's super easy to use it.
First of all: Umbraco. It has a compiled global.asax out of the box and you can't actually go and extend it. In order to register the routing for the Web API you would normally have to edit the global.asax. Luckily, you can implement this easily using the System.Web.PreApplicationStartMethod.
So in Umbraco I have my Umbraco.Web project which is often completely uncompiled, so it's a Web Site Project instead of a Web Application Project (although I'm changing this). Next to that I usually have a seperate project called CustomerName.Extensions in which I add custom code that is compiled and the DLL ends up in Umbraco's bin folder.
Note: if you want to follow along, ASP.NET MVC 4 should be installed (it includes the Web API) on your machine (not in Umbraco) or you could install the Web API nuget package.
In your (Umbraco) web project, you will also need a reference to Json.NET, again, just install the nuget package.
To start with, I add references to that Extensions project:
- System.Web
- System.Web.Http
- System.Web.Http.WebHost
Then I add a class to register the API routes on startup, for me it looks like this:
using System.Linq; using System.Web.Routing; using System.Web.Http; using CustomerName.Extensions; [assembly: System.Web.PreApplicationStartMethod(typeof(AppStart), "PreStart")] namespace CustomerName.Extensions { public static class AppStart { public static void PreStart() { RouteTable.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); } } }
To test if the Web API bits work, add a new item to your project, I don't know about VS2010 but in VS2012 you can just find the Web API Controller Class right there.
If you can't pick that one, then just make a new class and have it inherit from ApiController (as pictured above as well).
The default API Controller will give you some methods and you can immediately compile and check it out.
So in my case, I go to: http://mysite.local/api/values and get a lovely XML feed:
And.. that's all there is to it!
20 comments on this article
Really good call. But one question did you integrated Umbraco 4.7.2 project with MVC4 first before doing these steps?
Thank Pinal! No this was on a pure Umbraco 4.7.0 project, nothing installed beforehand. It will work equally well on any other Webforms project.
Hi Sebastiaan, This functionality works fine in umbraco 4.9 for me. But what about 4.10 after modifications in request pipelines.
I tried to implement this in U4.10 but not working out.
Thanks & Regards
Pinal Bhatt
@Pinal it seems to work fine for me, although I did have to add ~/api/ to the umbracoReservedPaths (web.config). So if your routes also start with /api/ then try that.
yup... that trick worked. so with U4.10 we need to add ~/api/ to umbracoReservedPaths (web.config).
Thanks a lot.
I'll ask Shannon if this can be fixed in 4.11.0 somehow, we've been talking about a similar scenario before so I think he might be able to make it work again.
Also, for your information, we're now offering a new way to register routes that might be handier for you, read about it here:
http://issues.umbraco.org/issue/U4-1173
Was also able to run this by overriding OnApplicationStarted in my new custom global.asax after inheriting it from Umbraco.Web.UmbracoApplication instead of using System.Web.PreApplicationStartMethod.
Once again thanks a lot for this wonderful concept.
Hmmm, I'm not sure you were intended to do that. Be advised that we're recommending the method mentioned in that issue instead, so you might want to rewrite it a tiny bit. :-)
sorry but could not get you.
Which method you suggest: Using System.Web.PreApplicationStartMethod or overriding OnApplicationStarted?
Look at the issue I posted:
http://issues.umbraco.org/issue/U4-854
So I meant using IApplicationEventHandler is the recommended way forward from now on.
How about a zipped solution of the project shown above, for us newbies?
@Anon I don't use this approach any more, but you should look at this one, it will have all you need:
http://our.umbraco.org/projects/developer-tools/umbraco-visual-studio-project
Hmm, commented on the wrong post =)
90% of this post is the basic Web API "hello world" project, it's not hard to follow along..
But here goes:
https://dl.dropbox.com/u/3006713/Umbraco4111WebAPISample.zip
Make sure you have NuGet installed in Visual Studio and that package restore is allowed in the Package Manager settings.
Backoffice username: admin password: admin
That's awesome, thanks a mil!
Hi Sebastiaan
Do you know the best way to utilise WebAPI v Standard Controllers in Umbraco v6.
Been trying to get WEBApi to play nicely in a separate project for v6 like you have in this article and not having a great deal of luck at the moment.
Thanks
@Damian Register the route like so:
https://gist.github.com/nul800sebastiaan/4755162
In web.config add ~/api in the umbracoReservedPaths and create an ApiController class like above (just start with the default example to test if it works and go to /api/values).
Hi Sebastiaan
I'm using v6 , added the ~/api/ in the web.config, registered the route from https://gist.github.com/nul800sebastiaan/4755162 and created the apiController but It still not working and I get: The resource cannot be found, HTTP 404.
do you see that I missed something?
Aweome. I actually game back to say i managed to get it working using the AppStart example for the old version but saw your reply.
Gave it a whirl and it works great - and much cleaner! :)
Thanks Sebastiaan.
For anyone that may have issues with this on V6...
I was not able to get this to work. I was getting a 404 when i would try to browse to my api url. The problem was that i was using IApplicationStartupHandler. This is referenced by the url above (https://gist.github.com/nul800sebastiaan/4755162). However, if you download sabastiaan's sample project, he is using IApplicationEventHandler. Once i switched to that, all worked fine.
May want to get that git doc updated...
Just also wanted to thank Sabastiaan for taking the time to write up this article! Good read!