How do I route a URL with a querystring in ASP.NET MVC?

前端 未结 4 1732
有刺的猬
有刺的猬 2020-11-28 15:28

I\'m trying to setup a custom route in MVC to take a URL from another system in the following format:

../ABC/ABC01?Key=123&Group=456

The 01

4条回答
  •  猫巷女王i
    2020-11-28 16:15

    When defining routes, you cannot use a / at the beginning of the route:

    routes.MapRoute("OpenCase",
        "/ABC/{controller}/{key}/{group}", // Bad. Uses a / at the beginning
        new { controller = "", action = "OpenCase" },
        new { key = @"\d+", group = @"\d+" }
        );
    
    routes.MapRoute("OpenCase",
        "ABC/{controller}/{key}/{group}",  // Good. No / at the beginning
        new { controller = "", action = "OpenCase" },
        new { key = @"\d+", group = @"\d+" }
        );
    

    Try this:

    routes.MapRoute("OpenCase",
        "ABC/{controller}/{key}/{group}",
        new { controller = "", action = "OpenCase" },
        new { key = @"\d+", group = @"\d+" }
        );
    

    Then your action should look as follows:

    public ActionResult OpenCase(int key, int group)
    {
        //do stuff here
    }
    

    It looks like you're putting together the stepNo and the "ABC" to get a controller that is ABC1. That's why I replaced that section of the URL with {controller}.

    Since you also have a route that defines the 'key', and 'group', the above route will also catch your initial URL and send it to the action.

提交回复
热议问题