简体   繁体   中英

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

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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM