Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get this ASP.NET routing to work properly?

I'm trying to write a really simple prototype of a CMS system using ASP.Net MVC 3.

The system has a single controller with two actions: show and create.

The format for the 'Show' action is meant to take up to 5 optional parts, e.g. domain.com/part1/part2/part3/part4/part5

The format for the 'Show' action is similar, but should have a leading 'create' part e.g. domain.com/create/part1/part2/part3/part4/part5

I have the following settings in my global.asax:

    routes.MapRoute(
        "CreatePageRoute",
        "create/{part1}/{part2}/{part3}/{part4}/{part5}",
        new
            {
                controller = "Page",
                action = "Create",
                part1 = UrlParameter.Optional,
                part2 = UrlParameter.Optional,
                part3 = UrlParameter.Optional,
                part4 = UrlParameter.Optional,
                part5 = UrlParameter.Optional
            });

    routes.MapRoute(
        "Default",
        "{part1}/{part2}/{part3}/{part4}/{part5}",
        new
            {
                controller = "Page",
                action = "Show",
                part1 = UrlParameter.Optional,
                part2 = UrlParameter.Optional,
                part3 = UrlParameter.Optional,
                part4 = UrlParameter.Optional,
                part5 = UrlParameter.Optional
            }
        );

If my 'Show' method fails to find a page matching the supplied path, it returns a 'not found' page that includes an options to create a new page with the supplied path. This link is defined using the following:

@Html.ActionLink("Yes", "Create") 

[the "Yes" represents the answer to the question "do you want to create a page for this path?"]

So when testing the 'Default' route, I see that my 'Show' action is called successfully for all of the following paths: {empty} a a/b a/b/c a/b/c/d a/b/c/d/e

which is great.

However, the resulting 'create' link generated by the statement "@Html.ActionLink("Yes", "Create")" gives inconsistent results. It seems to generate a different hyperlink depending on the length of the input. The results are as follow:

For path "{empty}", the link offers "localhost{:port}/" - I was hoping for "localhost{:port}/create"

For path "/a", the link offers "localhost{:port}/" - I was hoping for "localhost{:port}/create/a"

For path "/a/b", the link offers "localhost{:port}/a" - I was hoping for "localhost{:port}/create/a/b"

For path "/a/b/c", the link offers "localhost{:port}/a/b" - I was hoping for "localhost{:port}/create/a/b/c"

For path "/a/b/c/d", the link offers "localhost{:port}/create/a/b/c/d" - which is what I was hoping for

For path "/a/b/c/d/e", the link offers "localhost{:port}/create/a/b/c/d/e" - which is what I was hoping for

Why does it only work when I supply "/a/b/c/d" or "/a/b/c/d/e"?

I know I'm being idiotic, please help me

Sandy

like image 339
sandy Avatar asked Nov 15 '22 03:11

sandy


1 Answers

I believe that multiple optional parameters are causing the discrepency.
How about:

    routes.MapRoute("Create5", "create/{part1}/{part2}/{part3}/{part4}/{part5}", new { controller = "Page", action = "Create", part5 = UrlParameter.Optional });
    routes.MapRoute("Create3", "create/{part1}/{part2}/{part3}", new { controller = "Page", action = "Create", part3 = UrlParameter.Optional });
    routes.MapRoute("Create1", "create/{part1}", new { controller = "Page", action = "Create", part1 = UrlParameter.Optional });

    routes.MapRoute("Default5", "{part1}/{part2}/{part3}/{part4}/{part5}", new { controller = "Page", action = "Show", part5 = UrlParameter.Optional });
    routes.MapRoute("Default3", "{part1}/{part2}/{part3}", new { controller = "Page", action = "Show", part3 = UrlParameter.Optional });
    routes.MapRoute("Default1", "{part1}", new { controller = "Page", action = "Show", part1 = UrlParameter.Optional });
like image 162
RP Niemeyer Avatar answered Dec 31 '22 18:12

RP Niemeyer