Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC 3 RESTful Routing: RouteData must contain an item named Action?

We are using the ASP.NET MVC Futures project (Microsoft.Web.Mvc) to enable an MVC 3 application to use RESTful routes. This application has been working perfectly under MVC 1 and its related System.Web.Mvc.Resources.dll assembly for the same functionality.

We are registering the routes as such:

routes.MapResourceRoute("MyController", "{MyItemId}");

Which should give us routes like:

/MyController
/MyController/{MyItemId}
/MyController/{MyItemId}/EditForm
/MyController/CreateForm

We get three of the four routes that are valid -- the second on that list (/MyController/{MyItemId}) returns an error:

Server Error in '/' Application.
The RouteData must contain an item named 'action' with a non-empty string value. 

When I try appending ?action=Details or other ways of injecting an action parameter to the URL, I get 404 errors. It looks like the WebEnabledApi attribute in the Futures code changed significantly - anyone else having these issues, and have a solution?

like image 738
rswafford Avatar asked Jan 26 '11 20:01

rswafford


1 Answers

The default route is based on "controller/action/id", as you're just passing the id and not the action, it will give you 404 error.

routes.MapRoute("Default", "{controller}/{action}/{id}",
            new
            {
                controller = "Home",
                action = "Index",
                id = UrlParameter.Optional
            });

I believe you're trying to call the details action.

If you do have a action on the controller, using the default route might work:

MyController/Details/{MyItemId}
like image 191
Rômulo Spier Avatar answered Oct 11 '22 11:10

Rômulo Spier