【发布时间】:2018-05-29 09:44:27
【问题描述】:
我有一个测试控制器,其中我有一个接受custid 作为参数的索引操作。
这就是我的控制器的外观
public class TestController : Controller
{
// GET: Test
public ActionResult Index(int custid)
{
return View();
}
}
我在 route.config 文件中添加了一个额外的路由语句。
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "custom1",
url: "{controller}/{id}",
defaults: new { controller = "Test", action = "Index", custid = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
所以当使用 localhost:50675/test/101 之类的 url 访问测试控制器操作时,会出现错误。错误信息说
参数字典包含参数“custid”的空条目 方法的不可为空类型“System.Int32” 'System.Web.Mvc.ActionResult 索引(Int32)' 在 'WebAPICRUD.Controllers.TestController'。可选参数必须是 引用类型,可为空的类型,或被声明为可选 范围。参数名称:参数
但是当使用 localhost:50675/test?custid=101 之类的 url 访问测试控制器操作时,不会出现错误。
所以我不明白代码中有什么错误。
我需要做的结果是我可以发出这个 url http://localhost:50675/test/101 应该可以工作。请指导我。谢谢
【问题讨论】:
标签: asp.net-mvc-5