开发者

Default Index Method behaviour in ASP.NET MVC

I have the following ActionMethod in UserController

public ActionResult Index(string id, string name, int? org)

When I navigate to > http://example.com/User , the above action method is invoked. Thats good.

However when I navigate to > http://example.co开发者_如何学编程m/User/1 , it can't find the resource. Shouldn't it navigate to the above action method with id = 1 and the rest as null ?

Routing in Global.asax:

context.MapRoute(   
    "Default",   
    "/{controller}/{action}/{id}",   
    new { action = "Index", id = UrlParameter.Optional }   
);


You will have to add those other parameters into your routing as well for them to ever get populated.

routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}/{name}/{org}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional, name = UrlParameter.Optional, org = UrlParameter.Optional } // Parameter defaults
            );

You can then navigate to http://yourdomain/User/Index/1

As name and org are optional you can also pass these in when you want

http://yourdomain/User/Index/1/fred

http://yourdomain/User/Index/1/fred/44


You should navigate to http://mysite.com/User/Index/1 instead of http://mysite.com/User/1

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜