【问题标题】:ASP.NET MVC Optional Page ParameterASP.NET MVC 可选页面参数
【发布时间】:2016-10-26 20:18:19
【问题描述】:

假设我有两个网址

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)

【问题讨论】:

标签: c# asp.net asp.net-mvc


【解决方案1】:

我怀疑这可能是您唯一需要的路线:

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

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

【讨论】:

  • 谢谢,我的代码有两个问题,我注意到我有两个规则,而我只需要你指出的一个。其次,我有 id 作为可选,它应该是可选的。
猜你喜欢
  • 2010-12-14
  • 2020-12-04
  • 1970-01-01
  • 1970-01-01
  • 2014-08-31
  • 2013-12-17
  • 2011-08-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多