As we can pass integer from razor page to controller by asp-route-id , how can we pass a string instead?

Viewed 452

With the help of anchor tag

<a asp-controller="controller-name" asp-action="method-name" asp-route-id="1"> hyperlink </a>

it provokes the following in C#

controller-name/method-name/1

but how can I achieve the following?

controller-name/method-name/string
1 Answers

Firstlt,the default data format of string in url is controller-name/method-name/1 If you want to get string in action,you only need to use string Id rather than int Id.Here is a demo:

View:

<a asp-controller="Test" asp-action="Method" asp-route-id="1" asp-route-name="name1"> hyperlink </a>

url:

https://localhost:xxxx/Test/Method/1?name=name1

Controller:

public IActionResult Method(string Id, string Name) {
            return Ok();
        }

result: enter image description here

Related