开发者

Optional parameters in MVC3 routing

I am trying to create routes which can apply 1 and 2 type of URLs.

1 - First route will be at the start of application and I want 2 type of URLs that can used to access index page. I cannot hit below route when I have URL with Home at the end instead going to type 2.

http://www.example.com Or http://www.example.com/Home

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

2 - This type of URL is passing "Name" parameter to load contents from DB. I want this URL like

http://www.example.com/Page?name=Contact Or

http://www.example.com/Page?name=Contact&id=22

But I want above URL like

http://www.example.com/Contact Or http://www.example.com/About

Or

http://www.example.com/Contact/22 Or http://www.example.com/About/33

Where Contact and About are values for "Name" parameter passed in URL. Below is the Route used

 routes.MapRoute(
                "DynamicPages",
                "{name}",
                new { controller = "Home", action = "Page" }
   开发者_JAVA百科         );


Here is a possible solution. I am not sure if this is the right way to do this.

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    //To match http://www.mysite.com
    routes.MapRoute(
        "RootUrl",
        "",
        new { controller = "Home", action = "Index", 
                id = UrlParameter.Optional }
    );

    //To match http://www.mysite.com/Home
    routes.MapRoute(
        "RootUrlWithAction",
        "Home/{action}",
        new { controller = "Home", action = "Index" }
    );

    //To match http://www.mysite.com/Contact Or 
    //      http://www.mysite.com/About Or
    //      http://www.mysite.com/Contact/22 Or
    //      http://www.mysite.com/About/33
    routes.MapRoute(
        "DynamicPages",
        "{name}/{id}",
        new { controller = "Home", action = "Page", 
                id = UrlParameter.Optional }
    );

    // Everything else
    routes.MapRoute(
        "Default",
        "{controller}/{action}/{id}",
        new { controller = "Home", action = "Index", 
                    id = UrlParameter.Optional } // Parameter defaults
    );
}

Tested the following routes. Here the site root url is http://localhost:5879/. Refer the output screenshots provided below for each of the below mentioned scenario.

  1. http://localhost:5879/ --> Uses first route map
  2. http://localhost:5879/Home --> Uses second route map
  3. http://localhost:5879/Contact --> Uses third route map
  4. http://localhost:5879/About/33 --> Uses third route map
  5. http://localhost:5879/Home/Page?name=Contact&id=22 --> Uses third route map
  6. http://localhost:5879/Home/Index/2 --> Uses fourth route map

Screenshot #1:

Optional parameters in MVC3 routing

Screenshot #2:

Optional parameters in MVC3 routing

Screenshot #3:

Optional parameters in MVC3 routing

Screenshot #4:

Optional parameters in MVC3 routing

Screenshot #5:

Optional parameters in MVC3 routing

Screenshot #6:

Optional parameters in MVC3 routing

Hope that gives you some idea to solve your issue.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜