0

I have the following Actions in Controller

public ActionResult Index(int? pageNumber)
public ActionResult Details(string seoFriendlyName)

And I want to have the following routes:

~/article/ -> Action = Index, pageNumber = 1
~/article/page-5 -> Action = Index, pageNumber = 5
~/article/page-1 -> ~/article/
~/article/foo -> Action = Details, seoFriendlyName = foo

I've tried to define the following routes, but it doesn't work:

routes.MapRoute(
                null,
                "article/page-{pageNumber}",
                new { controller = "MyController", action = "Index", pageNumber = 1 },
                new[] { "MyNamespace" }
);

routes.MapRoute(
                null,
                "article",
                new { controller = "MyController", action = "Index", },
                new[] { "MyNamespace" }
);

routes.MapRoute(
                null,
                "article/{seoFriendlyName}",
                new { controller = "MyController", action = "Details", },
                new[] { "MyNamespace" }
);

Any help would be greatly appreciated!

2 Answers 2

2

Your requirements are self-contradictory. For example, what route should be selected if you have an article with seoFriendlyName == "page-6" and actual pageNumber == 2?

I'd suggest you change your requirements to

~/article/ -> Action = Index, pageNumber = 1
~/article/page/5 -> Action = Index, pageNumber = 5
~/article/page/1 -> ~/article/
~/article/foo -> Action = Details, seoFriendlyName = foo

and then you will have the following routes:

routes.MapRoute(
                null,
                "article/page/{pageNumber}",
                new { controller = "MyController", action = "Index", pageNumber = 1 },
                new[] { "MyNamespace" }
);

routes.MapRoute(
                null,
                "article",
                new { controller = "MyController", action = "Index", },
                new[] { "MyNamespace" }
);

routes.MapRoute(
                null,
                "article/{seoFriendlyName}",
                new { controller = "MyController", action = "Details", },
                new[] { "MyNamespace" }
);

UPDATE

In response to comments:

for that specific requirement you'll need to modify your routes that way:

routes.MapRoute(
                null,
                "article",
                new { controller = "MyController", action = "Index", pageNumber = 1 },
                new[] { "MyNamespace" }
);

routes.MapRoute(
                null,
                "article/page/{pageNumber}",
                new { controller = "MyController", action = "Index", },
                new[] { "MyNamespace" }
);


routes.MapRoute(
                null,
                "article/{seoFriendlyName}",
                new { controller = "MyController", action = "Details", },
                new[] { "MyNamespace" }
);
Sign up to request clarification or add additional context in comments.

2 Comments

If I call Url.Action("Index", "MyController") then ~/article/page will be rendered instead of ~/article
If you want this to be ~/article then you'll need to change routes order. Also, as Jan suggested, you should move your default pageNumer setting to another route. I'll update my answer in a moment.
0

You havn't told us, whats exactly not working. The only thing i have seen is, that you set wrong values to your pageNumber parameter.

This should work better:

routes.MapRoute(
    null,
    "article",
    new { controller = "MyController", action = "Index", pageNumber = 1 },
    new[] { "MyNamespace" }
);

routes.MapRoute(
    null,
    "article/page-{pageNumber}",
    new { controller = "MyController", action = "Index"},
    new[] { "MyNamespace" }
);

routes.MapRoute(
    null,
    "article/{seoFriendlyName}",
    new { controller = "MyController", action = "Details" },
    new[] { "MyNamespace" }
);

2 Comments

In your sample, if I call Url.Action("Index", "MyController", new { pageNumber = 1 }) that will be rendered as ~/article/page-1 and I want it to be rendered as ~/article
Yes, change the order of the first to routes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.