MVC 中的 UrlParameter 始终为 null
UrlParameter is always null in MVC
我无法在 MVC 中获取 UrlParameter 的值。我认为除了一件事之外一切都设置正确。
这是我的地图路线
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Banyo", // name it!
"{controller}/{action}/{Filtre}", // Route name
new { controller = "Banyo", action = "Marka", Filtre = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"Home", // Route name
"{action}/{Filtre}", // URL with parameters
new { controller = "Home", action = "Index", Filtre = UrlParameter.Optional }
);
}
这里是动作函数...
public ActionResult Marka(string Filtre = null)
{
return View();
}
和 Filtre returns 始终为空,即使我输入 url "http://localhost:7555/Banyo/Marka/Seranit" 也是如此。我期待获得 "Seranit" 值 Filtre 参数。
如果我输入"localhost:7555/Banyo/Marka?Filtre=Seranit"; ... Filtre 参数按预期为我提供了值 "Seranit"。我该如何解决这个问题?
我复制了您的路由配置并创建了一个控制器,就像您所做的那样,并且成功了。你有默认路由吗?它是在自定义路由之前定义的吗?考虑到如果之前定义了默认路由,则默认路由将首先捕获它。让我知道,以便我可以帮助你。
我无法在 MVC 中获取 UrlParameter 的值。我认为除了一件事之外一切都设置正确。
这是我的地图路线
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Banyo", // name it!
"{controller}/{action}/{Filtre}", // Route name
new { controller = "Banyo", action = "Marka", Filtre = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"Home", // Route name
"{action}/{Filtre}", // URL with parameters
new { controller = "Home", action = "Index", Filtre = UrlParameter.Optional }
);
}
这里是动作函数...
public ActionResult Marka(string Filtre = null)
{
return View();
}
和 Filtre returns 始终为空,即使我输入 url "http://localhost:7555/Banyo/Marka/Seranit" 也是如此。我期待获得 "Seranit" 值 Filtre 参数。
如果我输入"localhost:7555/Banyo/Marka?Filtre=Seranit"; ... Filtre 参数按预期为我提供了值 "Seranit"。我该如何解决这个问题?
我复制了您的路由配置并创建了一个控制器,就像您所做的那样,并且成功了。你有默认路由吗?它是在自定义路由之前定义的吗?考虑到如果之前定义了默认路由,则默认路由将首先捕获它。让我知道,以便我可以帮助你。