MVC 4.5 Web API Routing not working?

2019-01-19 07:50发布

The 1st route works.

e.g. api/Shelves/SpaceTypes/1

The 2nd route doesn't work. I get multiple actions error.

e.g api/Shelves/1

Q) Why?

These are my routes:

config.Routes.MapHttpRoute(
    "DefaultApiWithAction",
    "api/{controller}/{action}/{id}"
);

config.Routes.MapHttpRoute(
    "DefaultApiWithId",
    "api/{controller}/{id}",
    null,
    new { id = @"\d+" }
);

This is my controller:

public HttpResponseMessage Get(int id)
{
     ...
}

[ActionName("SpaceTypes")]
public HttpResponseMessage GetSpaceTypes(int id)
{
     ...
}

5条回答
迷人小祖宗
2楼-- · 2019-01-19 08:15

For MVC 4.5 this is the only thing that works

There is currently a bug about this.

In order to get your routing to work so the following work

api/Shelves/ //Get All Shelves
api/SpaceTypes/1 //Get Shelf of id 1
api/Shelves/1/SpaceTypes/  //Get all space types for shelf 1

you need to do the following.

Change your routing over to. (Note the default action..)

config.Routes.MapHttpRoute(
    name : "DefaultAPi",
    routeTemplate : "api/{controller}/{id}/{action}",
    defaults: new {id= RouteParameter.Optional, 
    action = "DefaultAction"}
);

In your controller change the base methods over to

[ActionName("DefaultAction")]
public string Get()
{
}

[ActionName("DefaultAction")]
public string Get(int id)
{
}

[ActionName("SpaceTypes")]
public string GetSpaceTypes(int id)
{
}

Now everything should work as expected..

Thanks to Kip Streithorst full this, for a full explanation

查看更多
男人必须洒脱
3楼-- · 2019-01-19 08:15

Make sure in your project's Global.asx file, that you've added

WebApiConfig.Register(GlobalConfiguration.Configuration);

into the Application_Start function.

查看更多
爱情/是我丢掉的垃圾
4楼-- · 2019-01-19 08:16

@Kristof is almost right. You should make your second route:

config.Routes.MapHttpRoute(
    "DefaultApiWithId", 
    "api/{controller}/{id}",
    new { action = "Get" },
    new { id = @"\d+ }
    );
查看更多
Fickle 薄情
5楼-- · 2019-01-19 08:33

This route does not know which action to bind to :

config.Routes.MapHttpRoute("DefaultApiWithId", "api/{controller}/{id}", null, new { id = @"\d+" });

Both of your methods are a valid candidate.
I'm not 100% clear what your setup is but in normal REST every resource has a controller, it seems like you have 1 controller with 2 resources.
To make it work in this setup you could force your second route to the get action like this :

 config.Routes.MapHttpRoute("DefaultApiWithId", "api/{controller}/{id}", null, new { id = @"\d+", action="Get" });
查看更多
Animai°情兽
6楼-- · 2019-01-19 08:36

I had a similar issue and discovered i wasn't calling MapHttpAttributeRoutes method in my WebApiConfig...

hope it helps, David

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        // Web API routes
        config.MapHttpAttributeRoutes();

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
    }
}
查看更多
登录 后发表回答