Question

I'm transitioning out of using WCF Data Services (since they're apparently dead), and attempting to build OData feeds using the newer Microsoft ASP.NET WebApi 2.1. I'm looking for a way to rename my feeds to be different than the class name.

I built out all my controllers, and now I'm trying to rename them just slightly to preserve the idea of set vs. single entities. (For example, the feed should be named WorkCategories, but the class name should be WorkCategory). Is this possible? I want to do something like this:

public static void Register(HttpConfiguration config)
{
    builder.EntitySet<EmailSequenceItem>("EmailSequenceItems");
    builder.EntitySet<EmailSequence>("EmailSequences");
    builder.EntitySet<WorkCategory>("WorkCategories"); 
    ...
    config.Routes.MapODataRoute("odata", "odata", builder.GetEdmModel());
    config.MapHttpAttributeRoutes();
}

My controller looks like this (built from templates): public class WorkCategoryController: ODataController { private dcMaintContext db = new dcMaintContext();

    // GET odata/WorkCategory
    [Queryable]
    public IQueryable<WorkCategory> GetWorkCategory()
    {
        return db.WorkCategories;
    }
    ...
}

But what I get when I rename any of the feeds is a 404 when navigating to http://localhost/odata/WorkCategories:

HTTP/1.1 404 Not Found
Message: No HTTP resource was found that matches the request URI 'http://localhost/odata/WorkCategories'."

MessageDetail: No type was found that matches the controller named 'WorkCategories'
Was it helpful?

Solution

The name of the controller should be same with the EntitySet name by default, which is WorkCategories. So other controller name except for WorkCategoriesController won't work unless you create your own IODataRoutingConvention.

For the method name, webapi has its default routing rule.

  • For get entityset, GetWorkCategories() and Get() will work.
  • For get entity, GetWorkCategory(int key) and Get(int key) will work.

If you want to customize the method name, you can use AttributeRouting in webapi 2.2. http://blogs.msdn.com/b/webdev/archive/2014/03/13/getting-started-with-asp-net-web-api-2-2-for-odata-v4-0.aspx

[ODataRoute("WorkCategories/WhateverName")]
public IQueryable WhateverName() {...}

OTHER TIPS

Aha! I found this just after I posted it. I just need to rename my controller class to WorkCategoriesController, and the 2 Queryable methods to GetWorkCategories instead of WorkCategory

 public class WorkCategoriesController : ODataController
 {
        private dcMaintContext db = new dcMaintContext();

        // GET odata/WorkCategory
        [Queryable]
        public IQueryable<WorkCategory> GetWorkCategories()
        {
            return db.WorkCategories;
        }

        // GET odata/WorkCategory(5)
        [Queryable]
        public SingleResult<WorkCategory> GetWorkCategories([FromODataUri] int key)
        {
            return SingleResult.Create(db.WorkCategories.Where(workcategory => workcategory.ID == key));
        }
...
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top