ASP.NET MVC 3 RESTful 路由:RouteData 必须包含名为 Action 的项目?

发布于 2024-10-14 14:24:36 字数 724 浏览 8 评论 0原文

我们正在使用 ASP.NET MVC Futures 项目 (Microsoft.Web.Mvc) 来使 MVC 3 应用程序能够使用 RESTful 路由。该应用程序在 MVC 1 及其相关的 System.Web.Mvc.Resources.dll 程序集下完美运行,具有相同的功能。

我们这样注册路由:

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

这应该给我们这样的路由:

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

我们得到四个有效路由中的三个 - 该列表上的第二个 (/MyController/{MyItemId}) 返回错误:

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

当我尝试附加 ?action 时=详细信息或向 URL 注入操作参数的其他方式,我收到 404 错误。看起来 Futures 代码中的 WebEnabledApi 属性发生了显着变化 - 其他人也遇到这些问题并有解决方案吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

混吃等死 2024-10-21 14:24:36

默认路由基于“controller/action/id”,因为您只传递 id 而不是操作,它会给您 404 错误。

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

我相信您正在尝试调用详细信息操作。

如果您确实在控制器上执行了操作,则使用默认路由可能会起作用:

MyController/Details/{MyItemId}

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}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文