Reputation: 697
I'm new to MVC coding, and have been at this issue for a couple days now. I'm having trouble setting up multiple routing schemes, and having them work as intended. Here is what I've got.
Framework
Model
What I'm wanting to do is be able to have 2 different routing schemes in place
So here is how I'm structuring the tags in the documents
For Products/Edit/Id
< a asp-controller="Products" asp-action="Edit" asp-route-id="@item.Id">Edit< /a>
For Products/Info/CategoryName
< a asp-controller="Products" asp-action="Info" asp-route-category="@item.CategoryName">@item.CategoryName< /a>
So the thing is, this will actually work, functionally, but my hyperlinks for the Products/Info/CategoryName get rendered as query strings rather than the more user friendly version, for instance one category is "Fireplaces", so my links for Info become
Products/Info?category=Fireplaces
instead of what I'm wanting
Products/Info/Fireplaces
How can I configure my routes so that the Controller/Action/Parameter call works for both? I've already tried adding specific routes to app.UseMvc(), and again they work functionally, but the Info links still render out as query strings.
Upvotes: 0
Views: 45
Reputation: 697
Ok, finally got to the bottom of it. Rather than trying to define routes the old way, with app.UseMvc(), I was able to use the new DataAnnotations in the Controller class to define the route, which resulted in creating user friendly links like I wanted, rather than the query string links. So for my Info() method in my controller class, I changed to look like
[HttpGet]
[ActionName("Info")]
[Route("Products/Info/{category}")]
public IActionResult Info(string category)
{
.....
return View(productCategory);
}
Upvotes: 1