šljaker
šljaker

Reputation: 7374

asp.net mvc and routing

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!

Upvotes: 0

Views: 150

Answers (2)

Jan
Jan

Reputation: 16032

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" }
);

Upvotes: 0

Sergii Kudriavtsev
Sergii Kudriavtsev

Reputation: 10487

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" }
);

Upvotes: 2

Related Questions