MVC WebAPI – Help, my Attributes do not work!

17 05 2013

When building applications for the “Internet of Things” MVC WebAPI is a great infrastructure for solutions on device as well as in the backend.

In nutshell, the trick trick is that controller endpoints, normally used by the Razor engine to request business logic for web pages, can be used as endpoints on the network remotely. these e.g. can be consumed by HTML 5 single page applications or services running on devices. This is a very interesting technology and quite an enabler.

I am currently working on a few new courses under the “HTML 5 for .NET developers” headline and ran into this interesting effect.

I created a controller to be my custom base controller:

using System.Web.Http; namespace MvcApplication1.Controllers {     public class CustomBaseController : ApiController    

{

         // Add logic here    

} }

It inherits from APIController to make it a WebAPI controller.

Next, I added a new controller in VS 2012 to inherit from:

using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace MvcApplication1.Controllers {     public class MyController : CustomBaseController    

{         //         // GET: /GetSomeResult/

         [HttpGet]        

[ActionName("GetSomeResult")]        

public string SomeResult()

{

const string SomeResult = "Result";

return SomeResult;

         }   

} }

 

At my method, I used the ActionName attribute to change the outward facing name of the method for configuration of name and access type and – unfortunately – got a strange result.

When calling the service it came back with this error:

image

MVC Routes were set correctly, so I was a bit puzzled at first.

A bug? Not probable looking at this basic functionality.

After playing around for a while I have found the cause:

It is the System.Web.MVC reference in the controller that is causing this behavior. This is added automatically, if one chooses Add| New Item| MVC 4 Controller Class in VS 2012, as I did.

In System.Web.Mvc types for MVC method attributes are defined. The problem is that this is not the correct reference for a WebAPI Controller, but for a standard Razor MVC Controller!

WebAPI controllers need to use identically named attribute types in System.Web.Http!

Due to the fact that I did not inherit directly, but via my custom base class (CustomBaseController), from APIController, nor the compiler or IDE did find this type ambiguity and did complain.

As soon as you replace System.Web.Mvc with the System.Web.Http reference, all works fine.

using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Http; namespace MvcApplication1.Controllers {     public class MyController : CustomBaseController

     {        

//

         // GET: /GetSomeResult/

         [HttpGet]

         [ActionName("GetSomeResult")]

         public string SomeResult()

         {

             const string SomeResult = "Result";

             return SomeResult;

         }

    } }

image

 

Alexander

Advertisements

Actions

Information

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




%d bloggers like this: