0

I am trying to build my ASP.NET MVC 4.5 project to use search engine friendly URLs. I am using the following route mapping.

routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}/{title}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional, title = UrlParameter.Optional }
);

The intention is so that I can create URLs like this:

Mysite.com/Home/Page/1/this-title-bit-is-just-for-show

but it fails and I have to use URLs like this:

Mysite.com/Home/Page?page=1

In case it matters, the Controller Action that this link points to is below:

public ActionResult Page(int page)
{
    PostModel pm = new PostModel(page);
    return View(pm);
}

And I am generating URLs like this:

<a href="@Url.Action("Page", "Home", new { page = 1 })">1</a>

Can someone tell me where I am going wrong?

MSOACC
  • 3,074
  • 2
  • 29
  • 50
  • Change the method to `public ActionResult Page(int id)` or change the route to `url: "{controller}/{action}/{page}/{title}",` And note that only the last parameter can be marked as `UrlParameter.Optional` –  Feb 18 '16 at 03:48
  • uh... you're asking an on-topic SEO-tagged question? That's gotta be wrong. Somehow. –  Feb 18 '16 at 13:59

1 Answers1

2

Instead of

<a href="@Url.Action("Page", "Home", new { page = 1 })">1</a>

Use

<a href="@Url.Action("Page", "Home", new { id = 1 })">1</a> //instead of page use id here

and change action method as shown :-

public ActionResult Page(int id) //instead of page use id here
{
    PostModel pm = new PostModel(id);
    return View(pm);
}
Kartikeya Khosla
  • 18,743
  • 8
  • 43
  • 69