2016-10-26 78 views
1

可以说,我已经两个Web地址ASP.NET MVC可选页参数

http://www.example.com/page/one 
http://www.example.com/page/one/subOne 

我如何让他们由同一个控制器来处理。目前正在处理顶端地址,但第二个正在由不显示页面的回发函数处理。

所以对于我的路由配置,我有

routes.MapRoute("PageRule", "page/{id}", new { Controller = "document", action = "Index", id = UrlParameter.Optional }); 
routes.MapRoute("Page2Rule","page/{id}/{misc}", new { Controller = "document", action = "Index", id = UrlParameter.Optional }); 

,并在控制器我有

// GET: Document 
public ActionResult Index(string id, string misc) 

// Post 
[HttpPost] 
public ActionResult postcomment(string gCaptcha,string txtName, string txtEmail, string txtMessage) 
+0

请尝试阅读本文:http://stackoverflow.com/questions/5061249/asp-mvc-使用两个可选参数进行路由 – David

回答

2

我怀疑这可能是你需要的唯一途径:

routes.MapRoute("PageRule", 
       "page/{id}/{misc}", 
       new 
       { 
        Controller = "document", 
        Action = "Index", 
        misc = UrlParameter.Optional 
       }); 

请注意,id没有可选项。关键是idmisc不能同时选择 - 只有路线中的最后一个参数可以是可选的

+0

谢谢,我的代码有两个错误,我注意到当我只需要你指出的一个时,我有两条规则。其次,我有id作为可选,它应该misc是可选的。 – MiscellaneousUser