Search code examples
asp.net-mvcasp.net-mvc-routing

MVC routing Issues with GUID as ID type


I have a slight issue with my routes that i am not 100% sure how to resolve.

I have a controller called Project and i'm trying to make the following url setup site.com/project/ (returns all the projects) site.com/project/GUID (site.com/Project/60724573-5949-48ee-873a-8f8e3ea7dd48) returns the single project (completely different view)

Project Controller:

[RequireHttps]
public class ProjectController : Controller
{
    [HttpGet]
    // Get: /Project
    public ActionResult Index()
    {
        // get list of projects and return list
        return View();
    }


    [Route("Project/{id:Guid}")]
    [HttpGet]
    // Get: /Project/ID
    public ActionResult Project(Guid id)
    {
        // get project object from DB based on ID passed
        //return View(model);
        return View();
    }
}

In My route i have attempted 2 route setups:

first:

routes.MapRoute(
                name: "Project",
                url: "{controller}/{action}",
                defaults: new {controller = "Project", action = "Index"}
            );

this allowed me to get /project/ to list all the projects

I started doing some research and found that I could do some work with routes by doing this.

            routes.MapRoute( 
           name: "AllProjects",
            url: "Project/",
            defaults: new
            {
                controller = "Project",
                action = "Index"
            });

I could then modify my controller to do something like this:

[Route("Project/")]
    [ActionName("AllProjects")]
    [HttpGet]
    // Get: /Project
    public ActionResult Index()
    {
        storeSession();
        // get list of projects and return list
        return View();
    }

    [Route("Project/{id}")]
    [ActionName("SingleProject")]
    [HttpGet]
    // Get: /Project/ID
    public ActionResult Index(Guid id)
    {
        storeSession();

        // get project object from DB based on ID passed
        //return View(model);
        return View();
    }

I seem to be at a small impasse I can get one to work but not the other (I can flip them with ease but i can't seem to get both to work.

I appreciate any advice you have on this and thanks in advance.


Solution

  • I managed to resolve my own issues.

    In my route I went back to:

                routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new {controller = "Project", action = "Index", id = UrlParameter.Optional}
    

    in my controller i had to do the following:

    [RequireHttps]
    [RoutePrefix("Project")]
    public class ProjectController : Controller
    {
    
        [Route]
        [HttpGet]
        // Get: /Project
        public ActionResult Index()
        {
    
            // get list of projects and return list
            return View();
        }
    
        [Route("{id}")]
        [HttpGet]
        // Get: /Project/ID
        public ActionResult Index(Guid id)
        {
    
    
            // get project object from DB based on ID passed
            //return View(model);
            return View();
        }