문제

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?

도움이 되었습니까?

해결책

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 }
);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top