gpt4 book ai didi

具有相同 Action 名称和不同参数的 C# MVC 路由

转载 作者:太空宇宙 更新时间:2023-11-03 19:56:20 24 4
gpt4 key购买 nike

我在 SurveyController 中有两个方法:

public ActionResult Index(string surveyId, string surveyWaveId, string programId)

public ActionResult Index(string hash)

我希望能够去:

调查/索引/{string} 调查/索引/{string}/{string}/{string}

并根据提供的参数数量路由到正确的操作。这在 MVC5 中可行吗?我的 RouteConfig 中也有这个。

routes.MapRoute(
name: "SurveyEmailLink",
url: "Survey/Index/{hash}",
defaults: new { controller = "Survey", action = "Index"},
namespaces: new[] { "Cobalt.Controllers" }
);

routes.MapRoute(
name: "SurveyIconLink",
url: "Survey/Index/{surveyId}/{surveyWaveId}/{programId}",
defaults: new { controller = "Survey", action = "Index" },
namespaces: new[] { "Cobalt.Controllers" }
);

routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "Cobalt.Controllers" }
);

提前致谢!

最佳答案

最好(也是最简单)的选择是为您的操作命名,以更多地说明它们的用途...除非 Index 确实是这两者的合适名称。

    routes.MapRoute(
name: "SurveyIconLink",
url: "Survey/Icon/{surveyId}/{surveyWaveId}/{programId}",
defaults: new { controller = "Survey", action = "Index" },
namespaces: new[] { "Cobalt.Controllers" }
);

routes.MapRoute(
name: "SurveyEmailLink",
url: "Survey/Email/{hash}",
defaults: new { controller = "Survey", action = "Index"},
namespaces: new[] { "Cobalt.Controllers" }
);

关于具有相同 Action 名称和不同参数的 C# MVC 路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33287966/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com