Search code examples
c#asp.netasp.net-mvcasp.net-mvc-routing

ASP.NET MVC: all controller values are passed null


I've added new action to my asp.net mvc application and add specific rule for it inside RouteConfig.cs.

But all parameters passed as null.

Here is my route rule:

routes.MapRoute(
     "toekn_submit_route",
    "{controller}/SendToken/{platform}/{token}/{uid}", 
    new { controller = "Home", action = "SendToken" }
    , new[] { "MvcApplication.Controllers" }
);

And here is action deceleration:

public JsonResult SendToken(string platform, string token, string uid) { ... }

I call action using this URL: http://localhost:51650/Home/SendToken/platform/token/uid


Solution

  • Order of how routes are added is important. First matched route wins.

    Make sure that this added route is added before more general routes otherwise they would be matched by another route that doesn't populate the placeholders as intended.

    routes.MapRoute(
        name: "token_submit_route",
        url: "{controller}/SendToken/{platform}/{token}/{uid}", 
        defaults: new { controller = "Home", action = "SendToken" },
        namespaces: new[] { "MvcApplication.Controllers" }
    );
    
    //...other more general routes.
    
    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );
    

    For example if the Default route was placed before the token route it would still match http://localhost:51650/Home/SendToken/platform/token/uid

    where

    controller = "Home", 
    action = "SendToken", 
    id = "platform/token/uid"