ASP.NET MVC 如何从URL读取参数?配置路由后code参数始终为null
ASP.NET MVC Action接收URL参数始终为null问题
核心目标
编写控制器Action时实现从URL读取参数,基于参数内容执行对应业务逻辑,但当前参数读取流程未生效。
现有实现代码
1. 控制器Index Action
public ActionResult Index(string code) { // 基于code参数执行业务处理 return View(); }
2. 路由配置规则
public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } ); routes.MapRoute( name: "Code", url: "{controller}/{action}/{code}", defaults: new { controller = "Home", action = "Index"} ); }
故障表现
无论怎么调整测试的URL地址,断点调试时发现传入Action的code参数始终为null。
已排查测试的无效URL
先后用以下地址发起请求,均未正常获取到参数值:
http://localhost:49218/?code=1234http://localhost:49218?code=1234http://localhost:49218/Index/?code=1234
内容的提问来源于stack exchange,提问作者Burdy
相关产品推荐
相关产品推荐

