ASP.net MVC routing

本文关键字:routing MVC net ASP | 更新日期: 2023-09-27 18:34:57

>我刚刚在我的控制器中创建了以下操作:

public ActionResult Serial(string letterCase)
{
    string serial = "SAM_ATM_1.0.0";
    if (letterCase == "lower")
    {
        return Content(serial.ToLower());
    }
    return Content(serial);
}

并在默认操作上方添加了以下路由规则:

routes.MapRoute(
    name: "Serial",
    url: "serial/{letterCase}",
    defaults: new { controller = "Home", action = "Serial", letterCase = "upper" }
);

但是,在调试会话中调用 url http://localhost:5532/home/serial/lower,letterCase传递时为 null 值。

ASP.net MVC routing

因为您调用 localhost:5532/home/serial/lower,所以尝试调用 localhost:5532/serial/lower

或者,如果您需要 localhost:5532/home/serial/lower,请将路由规则重写为

routes.MapRoute(
    name: "Serial",
    url: "home/serial/{letterCase}",
    defaults: new { controller = "Home", action = "Serial", letterCase = "upper" }
);