Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ASP.NET MVC可选页面参数_C#_Asp.net_Asp.net Mvc - Fatal编程技术网

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 )