本文介绍了MVC3重载指标的行动得到500错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些问题超载控制器Index操作。在控制器我有以下动作:

I having some problems overloading the Index action in a controller. In the controller I have the following actions:

public ActionResult Index(int id)
{
    return View();
}

public ActionResult Index()
{
    return View();
}

要或者URL(controllername /或controllername / 1),导致500错误。然而,当我使用:

Going to either URL (controllername/ or controllername/1) results in a 500 error. However when I use:

public ActionResult Index(int? id)
{
    return View();
}

该controllername / URL的作品,但controllername / 1的结果在404错误。我的Global.asax是pretty香草:

The controllername/ URL works but controllername/1 results in a 404 error. My global.asax is pretty vanilla:

routes.MapRoute(
    "Default", // Route name
    "{controller}/{action}/{id}", // URL with parameters
    new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);

我想要做的就是能够处理空ID以及值的整数ID。任何建议将不胜AP preciated。

What I would like to do is be able to handle a null id as well as an integer id value. Any suggestions would be greatly appreciated.

谢谢!

推荐答案

我想你会需要明确的是,路线:

I think you will need to explicit the routes for that:

routes.MapRoute(
    "ControllerName", // Route name
    "ControllerName/{id}", // URL with parameters
    new { controller = "ControllerName", action = "Index", id = UrlParameter.Optional }
);

如果它不工作,你可能需要更明确之前补充一点:

if it doesn't work, you may need to be more explicit and add this before:

routes.MapRoute(
    "ControllerName",
    "ControllerName",
    new { controller = "ControllerName", action = "Index"}
);

这篇关于MVC3重载指标的行动得到500错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-18 19:46