Question

Using ASP MVC, I have the following Action link,

<a href="@Url.Action("Page", "Content", new { publicationId = publication.PublicationId })">@publication.Title</a>

for the controller action

public ActionResult Page(int publicationId)

Which works, but the URL looks like this

Content/Page?publicationId=129

and I prefer it to look like

Content/Page/129

Is there anyway to make that change?

Was it helpful?

Solution

You need to define a route to use your publicationId parameter for your Content controller In your App_Start\RouteConfig.cs add above the Default route:

routes.MapRoute(
    name: "ContentPage",
    url: "Content/{action}/{publicationId}",
    defaults: new { controller = "Content", action = "Page", publicationId = UrlParameter.Optional }
);

If you want it to be specific only to the Page action:

routes.MapRoute(
    name: "ContentPage",
    url: "Content/Page/{publicationId}",
    defaults: new { controller = "Content", action = "Page", publicationId = UrlParameter.Optional }
);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top