开发者

ASP MVC Routing

开发者 https://www.devze.com 2022-12-28 17:12 出处:网络
now this is probably an stupid question but i\'m new to mvc and can\'t seem to get i开发者_高级运维t working.

now this is probably an stupid question but i'm new to mvc and can't seem to get i开发者_高级运维t working.

Here is what i would like to be able to do with the urls/routes:

1) www.domain.com/name/home/index

2) www.domain.com/home/index

where both the home controllers are seperate controllers and the name part will very but all must go to the same controller and the name should be an param for all the actions in there.

Is this at all possible? Thanks for your help.


This might not be the answer you're looking for, but I think that it would be more usual to see

www.domain.com/home/index
www.domain.com/home/index/name

My initial thinking was that an overloaded Index action method would make sense, but Daniel pointed out that this not allowed (at least not in the manner I suggested).

Updated answer...

Your Index action method could take a string name argument, and your routes would need to contain something like

routes.MapRoute(
    "Default",
    "{controller}/{action}/{name}",
    new { controller = "Home", action = "Index", name = "" }

In your action method a quick null check will tell you whether a name was included in the URL or not.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号