mvc-route.jpg

In this article you can find code example how to create new routing in mvc core within multilanguage application.

  1. Posted at26-5-2019 23:36:36
  2. bij Ori Slasi

creating new routing

if you want to change the way the links are in your blog controller and the links should be:
index: https://www.slasi/nl/blog
details: https://www.slasi.nl/nl/blog/routing-example-mvc-core-c-sharp

index that will show all the posts of the blog in one page.
details that will show the details of the blog.

we dont want to have in the details link the default mvc url: /nl/blog/details//blog/routing-example-mvc-core-c-sharp

here is what you need to do in order to achieve it:

in your startup.cs file add the following route:

routes.MapRoute(
                name: "blog-details",          
                template: "{language}/blog/{id}",
                defaults: new { controller = "blog", action = "details", language = defaultLanguage });

this will let you open the page/nl/blog/{id}


Fix the links to be blog/id

now we need to fix the links for each blog item in the /nl/blog page

if we will use the standard why we build a link:

<a class="nav-link" asp-controller="blog" asp-action="details" asp-route-id="routing-example-mvc-core-c-sharp">routing example mvc core c#</a> 

you will create a link like: nl/blog/details/{id} and that is not what we want.

if you take the action out it will not help as you will still get the index as default.


so what can we do is target the route name we have added in the startup.cs:

<"a asp-route="blog-details" asp-route-id="routing-example-mvc-core-c-sharp">

this will create the link without the details in the url and you will get: nl/blog/routing-example-mvc-core-c-sharp


live example you can see in this website.

if you have any questions or you need any help, you can contact us or send here feedback below


references:

https://webpifeed.blob.core.windows.net/webpifeed/Partners/Routing_1.x.pdf

https://docs.microsoft.com/en-us/aspnet/core/fundamentals/routing?view=aspnetcore-1.0


laat hier een reactie achter

je mailadres wordt hier niet zichtbaar. Deze velden zijn verplicht *

*
*
*

Ori Slasi

.NET Developer

23
POSTS


Contact

{{message}}