You need to enable JavaScript to run this app. ASP.NET MVC 如何从URL读取参数?配置路由后code参数始终为null
优惠活动
大模型
产品
解决方案
定价
更多

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=1234
  • http://localhost:49218?code=1234
  • http://localhost:49218/Index/?code=1234

内容的提问来源于stack exchange,提问作者Burdy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.11.04 22:15:03