A bug in the routing engine for .NET?
I have an ASP.NET MVC application. In the application, I have a bunch of similarly structured routes for different actions:
/Admin/Addresses/{AddressId}/Delete
/Admin/Phones/{PhoneId}/Delete
/Admin/Notes/{NoteId}/Delete
/Admin/Files/{FileId}/Delete
None of which work... I have been checking the routes and the actions for 5 hours now, and I know they are all written the way they should be, but it's still 404ing all of them.
The funny thing is that the following routes, which are also similar in structure work just fine:
/Admin/Addresses/{Id}/{Type}
/Admin/Phones/{Id}/{Type}
/Admin/Notes/{Id}/{Type}
/Admin/Files/{Id}/{Type}
The only difference between the two sets is that the delete routes use GET and are supposed to return JSON, while the othere ones use POST and redirect.
Has anyone ran into this before?
EDIT: Here's a bigger code sample per the requests on the comments. First code sample is of the ONLY working route (which is probably because it's the first in the list of routes to use the specified url structure) and the second is the next route in line, which isn't working at all...
Routes.MapRoute("Administration (Delete Employee)", "Administration/Employees/{EmployeeId}/Delete", new {
controller = "Administration",
action = "DeleteEmployee"
});
[AcceptVerbs(HttpVerbs.Get)]
public JsonResult DeleteEmployee(short EmployeeId) {
try {
db.DeleteEmployee(EmployeeId);
return Json(new IJsonStatus() {
Success = true
});
} catch (Exception ex) {
Shared.LogWarning(ex);
return Json(new IJsonStatus() {
Success = false,
Exception = ex.Message
});
};
}
And the non-working route:
Routes.MapRoute("Administration (Delete Address)", "Administration/Addresses/{AddressId}/Delete", new {
controller = "Administration",
action = "DeleteAddress"
});
[AcceptVerbs(HttpVerbs.Get)]
public JsonResult DeleteAddress(int AddressId) {
try {
db.DeleteAddress(AddressId);
return Json(new BaseRespon开发者_如何学编程se() {
Success = true
});
} catch (Exception ex) {
Shared.LogWarning(ex);
return Json(new BaseResponse() {
Success = false,
Exception = ex.Message
});
};
}
Probably could be useful to see your entire route mapping call rather than just a snippet. That stuff is very, very order of operations dependent.
Second, check out the MVC routing debugger. It helps demystify alot of route mystification issues.
Often times HTTP POST is used to request delete actions. Are you using POST on these actions that are decorated with [AcceptVerbs(HttpVerbs.Get)] ?
Try mapping it more like this:
RouteTable.Routes.Add(new Route(
"Administration/Forums/{action}/{id}",
new RouteValueDictionary(new { controller = "Forums", action = "Index", id = "" }),
new MvcRouteHandler()));
RouteTable.Routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = "" });
I added the debugger and it matched two routes:
Administration/Notes/{Id}/{Type} <--POST
Administration/Notes/{NoteId}/Delete <--GET
So, I assume that it matched the post route because of the Id in it, which really stands for either CustomerId or EmployeeId because it's a unified action which diferentiates based on the Type.
However, I would have expected the /Delete on the second route to force it into the real route, but I guess it stops at the first matching parameter?
精彩评论