Tuesday, September 16, 2014

WebAPI ODataController Error "No type was found that matches the controller named..."

In this post we fix the WebAPI ODataController's Error "No type was found that matches the controller named...". Which is not exactly an error, but is a message telling you that MVC could not find the type associated with the OData request received.

You are building an RESTful MVC Web API OData, enabled using an ODataController.
You mark with the  "EnableQuery" attribute the Action Methods, compile the application, check that the web service is working, by typing "$metadata" to the URI. Everything seems fine.
But....

"No type was found that matches the controller named..."



Why is that? If the OData REST service is working: why cannot find the data?

When you create the Web API OData Controller,  and make it inherit from ODataController, you MUST make sure that the name of the Controller corresponds to the Entity Set that you pass to the ODataConventionModelBuilder while you configured the application in the WebApiConfig file:


ODataModelBuilder builder = new ODataConventionModelBuilder();            builder.EntitySet<Note>("Notes");            config.MapODataServiceRoute(                routeName: "ODataRoute",                routePrefix: "OData",                model: builder.GetEdmModel());

If the name of the Entity is "Notes", the name of the Controller MUST be "Notes" :


WebAPI ODataController Error "No type was found that matches the controller named..."

Important: the NAME of the Controller MUST be identical to the name of the exposed EDM Entity : in our example , we must name it "NotesController".

That's why the WebAPI could not find a type corresponding to the URI OData query at the request .


Another CONVENTION that you must take into account, is the following:

Important:   An ODataController's Action method's name MUST comply with one of the HTTP verbs : "Get" is the name of the method for HTTP GET, "Post" is for HTTP POST, "Put" corresponds to the HTTP PUT verb, "Patch" is  for HTTP PATCH, "Delete" is for HTTP DELETE.


That's all!!! Enjoy OData Web API  !!!!

By Carmel Shvartzman

עריכה: כרמל שוורצמן

No comments:

Post a Comment