这个错误是非常非描述性的,但这里的关键是'ID'是大写的。这表明路由尚未正确设置。要让应用程序处理带有 id 的 URL,您需要确保至少为其配置了一条路由。您在路由配置.cs位于应用程序_启动文件夹。最常见的是将 id 作为可选参数添加到默认路由中。
public static void RegisterRoutes(RouteCollection routes)
{
//adding the {id} and setting is as optional so that you do not need to use it for every action
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
现在您应该能够按照您设置的方式重定向到控制器。
[HttpPost]
public ActionResult RedirectToImages(int id)
{
return RedirectToAction("Index","ProductImageManager", new { id });
//if the action is in the same controller, you can omit the controller:
//RedirectToAction("Index", new { id });
}
有一两次我通过正常重定向遇到了一些问题,不得不通过传递一个路由值字典。更多信息:带参数的 RedirectToAction https://stackoverflow.com/a/1300844/2960852
return RedirectToAction("Index", new RouteValueDictionary(
new { controller = "ProductImageManager", action = "Index", id = id } )
);
如果您收到非常相似的错误,但是小写的'id',这通常是因为路由需要一个尚未提供的 id 参数(调用没有 id 的路由/ProductImageManager/Index
). See 这样的问题 https://stackoverflow.com/questions/18052813/the-parameters-dictionary-contains-a-null-entry-for-parameter-id-of-non-nullab了解更多信息。