这是我的路线:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Sensor", action = "Index", id = UrlParameter.Optional }
);
}
这是我的行动:
public ActionResult Index(string id)
{
try
{
return View();
}
catch (Exception)
{
return View("Error");
}
}
}
当页面加载时,我得到这个网址:
http://localhost:2326/
现在我需要将参数传递给Index action:
http://localhost:2326/"123456"
当我这样做时,我得到了错误。
我猜路由存在一些问题。
如何将参数传递给索引操作?
答案 0 :(得分:1)
您可以使用以下方式传递多个参数:
http://localhost:2326/?id=123456¶m2=text¶m3=something
或
http://localhost:2326/Sensor/Index/123456/Param2/text/Param3/something
当然,您需要添加额外的路线来处理参数数量
routes.MapRoute(
"MoreParams",
"{controller}/{action}/{id}/{param2}/{param3}",
new { controller = "<controllername>", action = "Index", id = "", param2 = "", param3 = "" }
);
和方法。