Can I do {controller}\{id} with a Guid instead of {controller}\{action}\{id}

M Kenyon II

I'm looking to set up simple routes so that {controller}/{id} would default to {controller}/details/{id}. I've set it up like in this post: ASP.NET MVC 4 Routes - controller/id vs controller/action/id

But that post suggests for the routes to work I would need int {id} values. We use Guid ids.

Here is what I have:

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        routes.LowercaseUrls = true;
        routes.MapMvcAttributeRoutes(); 

        routes.MapRoute
        (
            name: "Detail",
            url: "{controller}/{id}",
            defaults: new { action = "Details", id = UrlParameter.Optional }
        );

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

Can I do this with Guid ids?

Shyju

The problem with your current route definition is, It will work for your particular request, but will break the existing default routing.When you try to access Home/About, it will send you to the Details action of Home!.

To solve this,what you can do is to pass a route constraint where you can pass the regex for Guid.

This should work fine.

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    routes.MapMvcAttributeRoutes();

    routes.MapRoute
        (
            name: "Detail",
            url: "{controller}/{id}",
            defaults: new { action = "Details", id = UrlParameter.Optional   },
            constraints: new { id = "[A-Z0-9]{8}-([A-Z0-9]{4}-){3}[A-Z0-9]{12}" }
        );

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

And change your action method parameter type to Guid.

public class ProjectController : Controller
{       
    public ActionResult Details(Guid id)
    {          
        return View();
    }
}

For the request yourSite/project/someValidGuid, now it will send the request to the Details action on ProjectController

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Using custom routes instead of /controller/action/{id}

From Dev

How do I get the standard expected Controller-Action-ID from Html Helper ActionLink instead of query string

From Dev

How do I pass link ID to controller?

From Dev

Can I use a controller in a page based on its controller ID?

From Dev

How can I get T4MVC to work with a route like "{controller}/{action}/{id}"?

From Dev

How do I pass a specific id to an MVC controller action from jQuery?

From Dev

How do I pass a specific id to an MVC controller action from jQuery?

From Dev

I cannot pass the id to the controller?

From Dev

Get current controller and action id in Yii

From Dev

Edit controller action is not receiving the Id form JQgrid

From Dev

Is it possible to reference the id attribute in the create action of a controller?

From Dev

How to pass id from view to Action on a controller

From Dev

How do I configure my Route config to get a URL Pattern like localhost/Product (Controller)/List (Action)/Category (Id)?

From Dev

WebApi Routing: api/{controller}/{id} and api/{controller}/{action} at the same time

From Dev

How do I get the ID of a post from another controller in Rails?

From Dev

How do I pass a newly created ID from the Order Controller to the Order Item Controller in MVC?

From Dev

Using Knockout.js how do i pass an id from a list in controller A to a button in another controller B

From Dev

How do I pass a newly created ID from the Order Controller to the Order Item Controller in MVC?

From Dev

How can I return a Json in controller action

From Dev

How do I set default Action for Controller?

From Dev

Which routes should I use in rails match :controller(/:action(/:id)) or resources?

From Dev

When should I create a separate controller instead of a custom action in Rails?

From Dev

When should I create a separate controller instead of a custom action in Rails?

From Dev

Mutiple buttons on MVC form - get item id in the action controller

From Dev

Parent id could not be accessed from child controller create action

From Dev

No route matches {:action=>"show", :controller=>"schools"} missing required keys: [:id]

From Dev

Yii2: How to log controller and action id for each request?

From Dev

No route matches {:action=>"edit", :controller=>"events"} missing required keys: [:id]

From Dev

Parent id could not be accessed from child controller create action

Related Related

  1. 1

    Using custom routes instead of /controller/action/{id}

  2. 2

    How do I get the standard expected Controller-Action-ID from Html Helper ActionLink instead of query string

  3. 3

    How do I pass link ID to controller?

  4. 4

    Can I use a controller in a page based on its controller ID?

  5. 5

    How can I get T4MVC to work with a route like "{controller}/{action}/{id}"?

  6. 6

    How do I pass a specific id to an MVC controller action from jQuery?

  7. 7

    How do I pass a specific id to an MVC controller action from jQuery?

  8. 8

    I cannot pass the id to the controller?

  9. 9

    Get current controller and action id in Yii

  10. 10

    Edit controller action is not receiving the Id form JQgrid

  11. 11

    Is it possible to reference the id attribute in the create action of a controller?

  12. 12

    How to pass id from view to Action on a controller

  13. 13

    How do I configure my Route config to get a URL Pattern like localhost/Product (Controller)/List (Action)/Category (Id)?

  14. 14

    WebApi Routing: api/{controller}/{id} and api/{controller}/{action} at the same time

  15. 15

    How do I get the ID of a post from another controller in Rails?

  16. 16

    How do I pass a newly created ID from the Order Controller to the Order Item Controller in MVC?

  17. 17

    Using Knockout.js how do i pass an id from a list in controller A to a button in another controller B

  18. 18

    How do I pass a newly created ID from the Order Controller to the Order Item Controller in MVC?

  19. 19

    How can I return a Json in controller action

  20. 20

    How do I set default Action for Controller?

  21. 21

    Which routes should I use in rails match :controller(/:action(/:id)) or resources?

  22. 22

    When should I create a separate controller instead of a custom action in Rails?

  23. 23

    When should I create a separate controller instead of a custom action in Rails?

  24. 24

    Mutiple buttons on MVC form - get item id in the action controller

  25. 25

    Parent id could not be accessed from child controller create action

  26. 26

    No route matches {:action=>"show", :controller=>"schools"} missing required keys: [:id]

  27. 27

    Yii2: How to log controller and action id for each request?

  28. 28

    No route matches {:action=>"edit", :controller=>"events"} missing required keys: [:id]

  29. 29

    Parent id could not be accessed from child controller create action

HotTag

Archive