Search code examples
c#asp.netmodel-view-controllerrouteconfig

Route config trims all the params in the get request


I have a route config in MVC project:

routes.MapRoute(
                name: "Client",
                url: "Client/{webUi}/{lang}/{*controllerUrl}",
                defaults: new
                {
                    controller = "Client",
                    action = "Index",
                    lang = "en-GB"
                }
            );

I have a ClientController with method Index().

public ActionResult Index(string webUi, string lang, string controllerUrl)
        { ... }

I am trying to use URL:

Client/ZWS/{lang}/ImportBundles?from=bundle

However, when I am debugging the ClientController, I am getting that the value in controllerUrl is ImportBundles. The parameters from=bundle is just stripped away.

Is it possible to achieve?

Thanks in advance!

Edit:

  1. I just tried to use this URL:

    Client/ZWS/{lang}/ImportBundles/from/bundle

And it worked. But is it possible to use this format:

Client/ZWS/{lang}/ImportBundles?from=bundle

Solution

  • Not exactly, but you can access everything after the ? inside your Action by accessing Request.QueryString.

    Request.QueryString["from"] //would evaulate to "bundle"
    

    You don't need the * for the catch-all in route definition to do that. If your format will always be Client/ZWS/{lang}/ImportBundles?from=bundle, you can get rid of it at the end of your route definition

    ...
    url: "Client/{webUi}/{lang}/{controllerUrl}",
    

    and controllerUrl will still evaluate to "ImportBundles"