【发布时间】:2012-11-01 20:53:23
【问题描述】:
我有这个带有以下代码的 global.asax 文件:
routes.MapRoute("Invitations",
"api/invitations",
new { controller = "Invitations", action = "Invitation"});
我有一个控制器:
public class InvitationsController : Controller
{
[HttpPut]
public JsonResult Invitation(InvitationResponse invitationResponse)
{
//code
}
}
我正在通过 HttpWebRequest 使用此 URL 访问控制器:
"http://localhost:6055/API/Invitations"
当我运行它时,我收到错误“NotFound”。
编辑:
全球航线:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute("Users",
"api/users/{id}",
new { controller = "Users", action = "UserAction", id = UrlParameter.Optional });
routes.MapRoute("CheckIn",
"api/checkins",
new { controller = "CheckIn", action = "CheckIn" });
routes.MapRoute("Appointments",
"api/appointments/{id}",
new { controller = "Appointments", action = "Appointment", id = UrlParameter.Optional });
routes.MapRoute("UserAppointments",
"api/users/{userId}/appointments/{startDate}",
new { controller = "Appointments", action = "UserAppointments", startDate = UrlParameter.Optional });
routes.MapRoute("UserInvitations",
"api/users/{userId}/invitations",
new { controller = "Invitations", action = "UserInvitations" });
routes.MapRoute("UserOverview",
"api/users/{id}/overview",
new { controller = "Users", action = "Overview" });
routes.MapRoute("Invitations",
"api/invitations",
new { controller = "Invitations", action = "Invitation"});
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
【问题讨论】:
-
我不知道this tester 是否适用于 MVC3(它适用于 MVC2)但您可以尝试一下,它是测试您的路由配置的好工具。
-
我测试了测试仪,我知道它可以计算当前的请求
-
那么你的路由似乎配置正确,你的请求应该命中控制器/动作。请求与路由层次结构中更高的任何其他路由不匹配?
-
我正在使用这些路线。您是否看到层次结构中更高的路线?
-
MVC 按顺序将请求与路由匹配,例如,如果您在前面放置一条通用路由(如“默认”)(即,将代码放在路由配置的顶部)。 MVC 将首先匹配它。问题是,在测试器中,您的“/API/Invitations”是否与您的“Invitations”路由之前的其他路由不匹配?
标签: asp.net-mvc-3 controller global-asax file-not-found maproute