当您在操作中操作数据时,您通常会收到一个 ID 作为参数,但您需要对该 ID 进行一些错误处理。您必须为每个操作执行的错误处理之一是确保 ID 大于 0(不是负数)。因此,我不想在操作中处理这个问题,而是想添加一个路由约束,所以如果它的 ID 为负数,就不会路由到该操作。
这是我的代码:
//route definition
routes.MapRoute(
"default route" ,
"{controller}/{action}/{id}" ,
new { id = UrlParameter.Optional },
new { id = @"^\d+$" }
);
//action definition (note I also tried with only [HttpPost] and with nothing same result
[AcceptVerbs(HttpVerbs.Post | HttpVerbs.Get )]
public ActionResult Edit( int id )
当您对操作执行GET时,一切正常,但是当我发布时,当它应该转到 404 页面时,我收到以下错误
HTTP verb POST used to access path '/object/edit/-2' is not allowed.
[HttpException (0x80004005): The HTTP verb POST used to access path '/object/edit/-2' is not allowed.]
System.Web.DefaultHttpHandler.BeginProcessRequest(HttpContext context, AsyncCallback callback, Object state) +740
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +632
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +194
有任何想法吗?也许是更好的解决方案?
编辑:刚刚注意到一些有趣的事情,我最初认为错误消息是 500,但它是“找不到方法”的 405