Asp.net-mvc – Generate links in ASP.NET MVC

asp.net-mvchyperlink

I have the following route definition in a MapRoute Table:

routes.MapRoute(
            "ViewDocument",
            "browse/document/{document_id}/{document_title}",
            new { controller = "Document", action = "ViewDocument"}
            );

I have to create links of documents on document index view (document object have "id" and "title" property)

What should be my approach to generating the link in ASP.NET MVC?

Is there anything I am doing wrong with the route definition?

Best Solution

In your routes:

routes.MapRoute(
    "ViewDocument",
    "browse/document/{document_id}/{document_title}",
    new { controller = "Document", action = "Title", document_id = "", document_title = ""}
);

In your View:

<%= Url.RouteUrl("ViewDocument", new { document_id = ... , document_title = ... }) %>

(renders plain url)

or

<%= Html.RouteLink("ViewDocument", new { document_id = ... , document_title = ... }) %>

(renders <a></a> element with href attribure filled with the url)