C# ASP.NET MVC可选页面参数
假设我有两个网址C# ASP.NET MVC可选页面参数,c#,asp.net,asp.net-mvc,C#,Asp.net,Asp.net Mvc,假设我有两个网址 http://www.example.com/page/one http://www.example.com/page/one/subOne // GET: Document public ActionResult Index(string id, string misc ) 如何让它们由同一个控制器处理。目前,顶部地址正在处理,但第二个地址正在由回发函数处理,该函数不呈现页面 // GET: Document public ActionResult Index(stri
http://www.example.com/page/one
http://www.example.com/page/one/subOne
// GET: Document
public ActionResult Index(string id, string misc )
如何让它们由同一个控制器处理。目前,顶部地址正在处理,但第二个地址正在由回发函数处理,该函数不呈现页面
// GET: Document
public ActionResult Index(string id, string misc )
所以对于我的路线配置,我有
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 )
在控制器里我有
// GET: Document
public ActionResult Index(string id, string misc )
及
// GET: Document
public ActionResult Index(string id, string misc )
我怀疑这可能是您需要的唯一路线:
// GET: Document
public ActionResult Index(string id, string misc )
routes.MapRoute("PageRule",
"page/{id}/{misc}",
new
{
Controller = "document",
Action = "Index",
misc = UrlParameter.Optional
});
请注意,
id
没有可选项。关键是id
和misc
不能同时是可选的-只有路由中的最后一个参数可以是可选的试着阅读本文:谢谢,我的代码有两个问题,我注意到我有两个规则,而我只需要你指出的一个。第二,我的id是可选的,它应该是可选的misc。
// GET: Document
public ActionResult Index(string id, string misc )