ASP.NET MVC 中的默认索引方法行为
有以下 ActionMethod
我在 UserController public ActionResult Index(string id, string name, int? org)
当我导航到 > 时 http://example.com/User ,调用上述操作方法。那挺好的。
但是,当我导航到 > http://example.com/User/1 ,找不到资源。难道它不应该导航到上面的 id = 1 的操作方法,其余的为 null 吗?
Global.asax 中的路由:
context.MapRoute(
"Default",
"/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
I have the following ActionMethod in UserController
public ActionResult Index(string id, string name, int? org)
When I navigate to > http://example.com/User , the above action method is invoked. Thats good.
However when I navigate to > http://example.com/User/1 , it can't find the resource. Shouldn't it navigate to the above action method with id = 1 and the rest as null ?
Routing in Global.asax:
context.MapRoute(
"Default",
"/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您还必须将这些其他参数添加到您的路由中才能填充它们。
然后,您可以导航到 http://yourdomain/User/Index/1
因为名称和组织是可选的,您 时也可以传递这些
当您需要http://yourdomain/User/Index/1/fred
http://yourdomain/User/Index/1/fred/44
You will have to add those other parameters into your routing as well for them to ever get populated.
You can then navigate to http://yourdomain/User/Index/1
As name and org are optional you can also pass these in when you want
http://yourdomain/User/Index/1/fred
http://yourdomain/User/Index/1/fred/44
您应该导航到 http://mysite.com/User/Index/1 而不是 http://mysite.com/User/1
You should navigate to http://mysite.com/User/Index/1 instead of http://mysite.com/User/1