Web API help page duplicating Action for all areas
Asked Answered
D

1

2

I'm working with Web API 2, and it seems to pull up my existing API calls already, except it's duplicating all the calls for each area that i have. For example, say i have 3 areas, and in one of those i have an API call that looks like:

public IList<string> GetStringList(string id)
    {
        //do work here...
        return new List<string>{"a","b","c"};
    }

if i have 3 areas, then the web api help page will show:

GET area1/api/MyAPIController/GetStringList/{id}

GET area2/api/MyAPIController/GetStringList/{id}

GET area3/api/MyAPIController/GetStringList/{id}

and the MyAPIController only exists in 'area2'. Why is this showing 3 times, and how can i fix it? If it helps, my area registration for area2 is:

public class Area2AreaRegistration : AreaRegistration
{
    public override string AreaName
    {
        get
        {
            return "Area2";
        }
    }

    public override void RegisterArea(AreaRegistrationContext context)
    {
        context.MapRoute(
            "Area2_default",
            "Area2/{controller}/{action}/{id}",
            new { action = "Index", id = UrlParameter.Optional }
        );

        context.Routes.MapHttpRoute(
    name: "Area2_ActionApi",
    routeTemplate: "Area2/api/{controller}/{action}/{id}",
    defaults: new { id = RouteParameter.Optional }
);

    }
}
Dmso answered 3/4, 2015 at 23:55 Comment(3)
Thought the issue might have something to do with devillers.nl/getting-webapi-and-areas-to-play-nicely , but that didn't end up fixing the issue unfortunatelyDmso
I'm seeing the same issue... were you able to figure it out?Watch
Nope, no answer yet. Hopefully will come up with something soonDmso
V
1

While not a solution to your problem, you can use attributes to map the routes for actions as a temporary workaround.

To enable attributes for routing add config.MapHttpAttributeRoutes(); to the registration in WebApiConfig, which should be within the App_Start folder.

public static void Register(HttpConfiguration config)
{
    // Attribute routing.
    config.MapHttpAttributeRoutes();

    // Convention-based routing.
    config.Routes.MapHttpRoute(
        name: "DefaultApi",
        routeTemplate: "api/{controller}/{id}",
        defaults: new { id = RouteParameter.Optional }
    );
}

Once you've enabled attribute routing you can specify a route over an action:

public class BooksController : ApiController
{
    [Route("api/books")]
    public IEnumerable<Book> GetBooks() { ... }
}

You can read more here. Take a look at route prefixes(shown above) and make sure you enable routing with attributes as shown in the beginning of the page.

Edit:

In your case:

[Route("area2/api/MyAPIController/GetStringList/{id}")]
public IList<string> GetStringList(string id)
{
    //do work here...
    return new List<string>{"a","b","c"};
}
Valdes answered 17/6, 2015 at 14:8 Comment(2)
I'm sorry, but i honestly don't know why you even suggest this. It doesn't reduce the amount of repeated calls above, which is my only issue. I'm not sure what other issue your suggestion is supposed to help solve. Additionally, i'm not a huge fan of attribute routing regardless: maxtoroq.github.io/2014/02/why-aspnet-mvc-routing-sucks.htmlDmso
I don't like the attribute routing either, but it did remove the duplicate calls I was receiving from multiple areas. I don't consider my answer to be a solution to your problem, just a work-around.Valdes

© 2022 - 2024 — McMap. All rights reserved.