Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么这条路线不能正常工作?_C#_Asp.net Mvc_Model View Controller - Fatal编程技术网

C# 为什么这条路线不能正常工作?

C# 为什么这条路线不能正常工作?,c#,asp.net-mvc,model-view-controller,C#,Asp.net Mvc,Model View Controller,我真的快疯了 下面是我的Global.asax中的内容 routes.MapRoute("BlogDetails", "Blogs/{id}/{title}", new { controller = "Blogs", action = "Details", id = "" }); routes.MapRoute( "Default", // Route name "{controller}/{action}

我真的快疯了

下面是我的Global.asax中的内容

routes.MapRoute("BlogDetails", "Blogs/{id}/{title}", new { controller = "Blogs", action = "Details", id = "" });
routes.MapRoute(
"Default",                                              // Route name
"{controller}/{action}/{id}",                           // URL with parameters
new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
);
这是我仅有的两条路线

当我尝试访问

它不起作用,我收到这个错误

参数字典包含“mysite.Controllers.BlogsController”中方法“System.Web.Mvc.ActionResult Details(Int32)”的不可为null类型“System.Int32”的参数“id”的null条目。可选参数必须是引用类型、可为null的类型或声明为可选参数

为什么这种情况不断发生

谢谢

我还应该添加如下所示的控制器代码

//
// GET: /Blogs/Edit/5

[Authorize]
public ActionResult Edit(int id)
{
  // do a bunch of stuff and return something
}
试试下面的方法

routes.MapRoute("BlogDetails", "Blogs/{id}/{title}",
 new { controller = "Blogs", action = "Details"},
 new { id = @"\d+" });
就MVC路由而言,
{id}
可以是任何东西(甚至是字符串),因此它将
Edit
匹配为字符串,不能进入操作的int
id

添加
new{id=@“\d+”}
作为额外参数告诉路由系统仅匹配数字


您拥有的两条路线实际上是“确认”的,在您的情况下,第一条路线被选中,而不是您预期的第二条

也许您需要修改路线以消除歧义:

routes.MapRoute("BlogDetails", "Blogs/{id}-{title}", new { controller = "Blogs", action = "Details", id = "" });
routes.MapRoute(
  "Default",                                              // Route name
  "{controller}/{action}/{id}",                           // URL with parameters
  new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
);
此定义将明确区分:
http://mysite/Blogs/Edit/1
http://mysite/Blogs/1-first


或者正如Baddie提到的,尝试向路由添加约束。

如果在第二条路由中删除
id
的默认值,会怎么样?仍然会给我同样的错误。它还中断了网站上的所有其他链接