mvc 参数字典包含空条目.....路由错误/问题
所以我有一个名为 Cars 的控制器,它有:
namespace MySite.Controllers
{
public class CarsController : ApplicationController
{
public ActionResult Index()
{
return View();
}
public ActionResult New()
{
return View();
}
[httppost]
public ActionResult New()
{
return View();
}
public ActionResult Details(int id)
{
return View();
}
}
}
Global asax
routes.MapRouteLowerCase(
"Cars", // Route name
"Cars/{id}", // URL with parameters
new { controller = "Cars", action = "Details", id = URLParameter.Optional}
);
routes.MapRouteLowerCase(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = URLParameter.Optional} // Parameter defaults
);
所以以下是我收到的错误:
参数字典包含非参数的空条目 方法中的可为空类型
:它让我重定向到 “详细信息” 视图,并要求一个 int,即使我想转到 “新建” 视图。我不太确定发生了什么事?我基本上想让我的网址小写,同时删除促销员路由上网址上的“操作”...这有意义吗? 任何帮助表示赞赏!谢谢!!!
So I have this controller called Cars, and it has:
namespace MySite.Controllers
{
public class CarsController : ApplicationController
{
public ActionResult Index()
{
return View();
}
public ActionResult New()
{
return View();
}
[httppost]
public ActionResult New()
{
return View();
}
public ActionResult Details(int id)
{
return View();
}
}
}
Global asax
routes.MapRouteLowerCase(
"Cars", // Route name
"Cars/{id}", // URL with parameters
new { controller = "Cars", action = "Details", id = URLParameter.Optional}
);
routes.MapRouteLowerCase(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = URLParameter.Optional} // Parameter defaults
);
So following is the error I'm getting:
the parameters dictionary contains a null entry for parameter of non
nullable type for method in
NOTE: It's getting me redirected to the "Details" View and is asking for an int even thought I wanted to go to the "New" view. I'm not really sure what's happening? I basically wanted to have my urls lowercased while removing the "action" on the url on the Promoter routing...Does that make sense?
Anyhelp is appreciated! Thanks!!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的路线无效
看一下第一个路线定义:
当您访问
Cars/New
时,第一个路线会被命中,因为所有参数很容易应用为:"Cars"
"Details"
"New"
如果您想要第一条路线定义仅覆盖某些 ID,您应该对其施加约束或更改你的路由。数字 ID 的约束应如下所示:
当您访问
Cars/New
时,不会命中第一条路线,因为New
不满足 ID 约束,因此路线处理将继续下一条路线(这会很好地解决它 - 正如它应该的那样)。请注意,
id
不再是可选的。如果对其施加约束,那就不可能了。如果您有一个Details
控制器操作,它很可能应该显示一些特定的详细信息。所以它实际上需要一些ID。Your routing is invalid
Take a look at the first route definition:
And when you access
Cars/New
this first route gets hit because all parameters are easily applied as:"Cars"
"Details"
"New"
If you'd like the first route definition to only cover certain IDs you should put a constraint onto it or change your routing. Constraint for numeric IDs should look like this:
When you'd access
Cars/New
first route wouldn't be hit becauseNew
doesn't satisfy ID constraint so route processing would continue with the next route (which would resolve it just fine - as it should).Mind the fact, that
id
isn't optional anymore. In case of putting a constraint onto it it can't be. If you have aDetails
controller action it should most probably display some certain details. So it actually needs some ID.如果您的路线设置如下,
则意味着任何以“/Cars/...”开头的 URL 都会将您重定向到“详细信息”操作,其中包括“/Cars/New”,并且它将尝试将“New”字符串映射到id 参数就是您收到错误的原因。
If your route is setted like this
that means that any URL that starts with "/Cars/..." will redirect you to Details action, that includes "/Cars/New" and it will try to map the "New" string to the id parameter that is why you get the error.