简体   繁体   English

ASP.NET MVC可选页面参数

[英]ASP.NET MVC Optional Page Parameter

Lets say I've two web addresses 可以说我有两个网址

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

How do I get them to be handled by the same controller. 如何使它们由同一控制器处理。 At the moment the top address is being handled but the second is being handled by the postback function which doesn't render the page. 目前正在处理最高地址,但是第二个地址正在由不呈现页面的回发功能处理。

So for my routes config, I have 所以对于我的路由配置,我有

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 });

and in the controller I have 在控制器中

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

and

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

I suspect this might be the only route you need: 我怀疑这可能是您唯一需要的路线:

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

Notice that there is no optional for id . 注意, id没有可选的。 The key is that both id and misc cannot be optional at the same time - only the last parameter in the route can be optional 关键是idmisc不能同时是可选的-路由中的最后一个参数可以是可选的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM