Asp.net-Mvc
如何在 ASP.NET MVC 中使用查詢字元串路由 URL?
我正在嘗試在 MVC 中設置自定義路由,以從另一個系統獲取以下格式的 URL:
../ABC/ABC01?Key=123&Group=456第二個 ABC 之後的 01 是一個步數,這將改變並且 Key 和 Group 參數將改變。我需要將此路由到控制器中的一個操作,其中步驟號鍵和組作為參數。我嘗試了以下程式碼,但是它引發了異常:
程式碼:
routes.MapRoute( "OpenCase", "ABC/ABC{stepNo}?Key={key}&Group={group}", new {controller = "ABC1", action = "OpenCase"} );例外:
`The route URL cannot start with a '/' or '~' character and it cannot contain a '?' character.`
您不能在路由中包含查詢字元串。嘗試這樣的路線:
routes.MapRoute("OpenCase", "ABC/ABC{stepNo}", new { controller = "ABC1", action = "OpenCase" });然後,在您的控制器上添加如下方法:
public class ABC1 : Controller { public ActionResult OpenCase(string stepno, string key, string group) { // do stuff here return View(); } }ASP.NET MVC 會自動將查詢字元串參數映射到控制器中方法中的參數。