Create links in ASP.NET MVC?

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 need to create links to documents in the index representation of the document (the document object has the properties "id" and "title")

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

Is there something I am doing wrong with determining the route?

+4
source share
3 answers

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 = ... }) %> 

(displays a simple URL)

or

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

(displays the <a></a> element with the href attribute populated by the url)

+6
source

Can't you find the correct document, simply based on its identifier?

Will there be header redundancy?

0
source

You can create links to documents for the route, indicated with the following:

 <%= Html.ActionLink("Doc Link", "Title", "Document", new { document_id="id", document_title="title" }, null) %> 

A few things to know about:

  • A custom route must be added before the default route.
  • You must specify the route values ​​as shown above so that they are indicated in the link.
0
source

Source: https://habr.com/ru/post/1285815/


All Articles