我有一個ASP.NET MVC應用程序。在應用方面,我有不同的動作一堆類似結構的路線:.NET路由引擎中的一個錯誤?
/Admin/Addresses/{AddressId}/Delete
/Admin/Phones/{PhoneId}/Delete
/Admin/Notes/{NoteId}/Delete
/Admin/Files/{FileId}/Delete
其中沒有工作......我一直在檢查5個小時的路線和動作,我知道他們都是以他們應該的方式寫下來,但它仍然是所有這些。
有趣的是,下面的路線,這也是類似結構的工作就好了:
/Admin/Addresses/{Id}/{Type}
/Admin/Phones/{Id}/{Type}
/Admin/Notes/{Id}/{Type}
/Admin/Files/{Id}/{Type}
兩個集合之間的唯一區別是,刪除路由使用GET和應該返回JSON ,而其他人使用POST和重定向。
有沒有人碰到過這個?
編輯:這是一個更大的代碼示例,每個請求的評論。第一個代碼示例是唯一的工作路線(這可能是因爲它是使用指定的網址結構的路線列表中的第一個),第二個代碼示例是線路中的下一條路線,根本不工作...
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
});
};
}
和非工作路線:
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 BaseResponse() {
Success = true
});
} catch (Exception ex) {
Shared.LogWarning(ex);
return Json(new BaseResponse() {
Success = false,
Exception = ex.Message
});
};
}
顯示您的代碼! – ChaosPandion 2009-10-15 23:30:02
@Alex:不要添加評論,編輯您的問題。 – 2009-10-15 23:43:50
只是一個註釋:我希望你能理解以HTTP GET方式實現刪除功能的所有含義。這是一個巨大的風險,因爲網絡爬蟲可以在一個簡單的過程中輕鬆消除所有數據。如果可能(使用Ajax),刪除操作應該使用HTTP DELETE來完成。 – 2009-10-16 16:23:26