TrinTrin
TrinTrin

Reputation: 470

Generate dynamic route path in asp.net core

I have created one .net core application that has below path for crud operations.

Create: http://localhost:1001/admin/123/app/456/user/Create

Update: http://localhost:1001/admin/123/app/456/user/Update

Select: http://localhost:1001/admin/123/app/456/user/Select

Writing below will solve the issue in the URL.

routes.MapRoute("CreateUser", "apps/{appId}/user/create",
  defaults: new { controller = "User", action = "Create" }); 

How to include the same in .cshtml file i.e <a asp-action="Create" asp-controller="User"> Create User </a>

The quick help appreciated.

Upvotes: 1

Views: 2106

Answers (1)

thirdDeveloper
thirdDeveloper

Reputation: 895

You can use asp-route-{value} option to indicate route values in anchor tag helper:

<a asp-action="Create" asp-controller="User" asp-route-adminID="@Model.AdminId" asp-route-appId="@Model.AppId"> Create User </a>

Please consider that I assumed that adminId and appId values are provided in the Model of the page. If it's wrong, set appropriate variable instead of those in your code.

For more information, you can check the Microsoft documentation: https://learn.microsoft.com/en-us/aspnet/core/mvc/views/tag-helpers/built-in/anchor-tag-helper?view=aspnetcore-3.0#asp-route-value

Upvotes: 1

Related Questions