Bitz Bitz - 25 days ago 11
C# Question

How do I create this route and use it?

Currently I am using the default route but I need a route that works like this:

localhost/Admin/Users/Index
localhost/Admin/Users/Add
localhost/Admin/Users/Delete


Where index add and delete are views with controllers in the AdminController.cs

The current structure everywhere else is fine, as it doesn't require multiple subdirectories.

Currently I have the the file I need to start with in:

{Project}/Views/Admin/Users/Index.cshtml


How would I create this route and how do I apply it to the controller?

Am I approaching this incorrectly?

Answer

This can be easily resolved using Route attributes, like:

[Route("Admin/Users/Edit/{id?}")]
public ActionResult TestView(int id)
{
    if (!String.IsNullOrEmpty(id))
    {
        return View(“OneUser”, GetUser(id));
    }
    return View(“AlUsers”, GetUsers());
}

MSDN: https://blogs.msdn.microsoft.com/webdev/2013/10/17/attribute-routing-in-asp-net-mvc-5/

Comments