ASP.net MVC 路由问题
在我的 ASP.Net MVC 应用程序中,我有以下控制器
HomeController
ExController
ExController 有一个接受字符串参数的方法:
public ActionResult Index(String id){....
使用参数,页面成功打开为: mysite .com/Ex/Index/my-string-value
但我希望它采用参数为: mysite.com/Ex/my-string-value
这是我的路由条目:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"Post",
"{controller}/{action}/{postId}",
new { controller = "Ex", action = "Index", postId="" }
);
我需要做什么才能将获取参数发送到 ExController打字mysite.com/Ex/GetParameter 而不是 mysite.com/Ex/Index/GetParameter。请帮忙。
In my ASP.Net MVC app, I have the following controllers
HomeController
ExController
ExController has this method that takes string parameters:
public ActionResult Index(String id){....
With parameters, the page opens successfully as: mysite.com/Ex/Index/my-string-value
but I want it to take parameters as: mysite.com/Ex/my-string-value
Here are my routing entries:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"Post",
"{controller}/{action}/{postId}",
new { controller = "Ex", action = "Index", postId="" }
);
What I need to do to send get parameters to ExController by typing mysite.com/Ex/GetParameter instead of mysite.com/Ex/Index/GetParameter. Please help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,您需要在默认路由之前定义Ex-route,否则默认将捕获所有路由。
其次,您可以简单地执行以下操作:
这将使您能够执行
www.mysite.com/Ex/GetParameter
您还需要将 ExController 上的 Index 操作更改为:
让 Modelbinder 正确绑定 postId 。
然后,这将依次调用操作 Index,传递
GetParameter
作为 postId希望这会有所帮助!
First of all you need to define the Ex-route before the default route, otherwise the default will catch all.
Second you can simply do this:
That will enable you to do
www.mysite.com/Ex/GetParameter
You also need to change you Index action on you ExController to:
to get the Modelbinder to bind postId correctly.
That will then in turn call the action Index passing
GetParameter
as the postIdHope this helps!