【发布时间】:2012-02-29 20:08:57
【问题描述】:
我的控制器中有一个动作如下。
//
// GET: /HostingAgr/Details/1
public ActionResult Details(int id)
{
HostingAgreement hosting = hmEntity.HostingAgreements.SingleOrDefault(h => h.AgreementId == id);
if (hosting == null)
{
TempData["ErrorMessage"] = "Invalid Agreement ID";
return RedirectToAction("Index");
}
return View(hosting);
}
现在如果我像下面这样调用 URL ..(用于测试目的)
/HostingAgr/Details/1fakeid
系统会抛出异常。
参数字典包含参数“id”的空条目 方法的不可空类型“System.Int32” 'System.Web.Mvc.ActionResult 详细信息(Int32)' 在 'HostingManager.Controllers.HostingAgrController'。一个可选的 参数必须是引用类型、可为空的类型或声明为 一个可选参数。参数名称:参数
因为 id 在 URL 参数中变成了字符串。我怎样才能在不引发系统错误的情况下处理这种情况?
【问题讨论】:
-
从标题中删除了
ActionResult,因为您谈论的是行动而不是结果。
标签: asp.net-mvc-3 asp.net-mvc-routing