ASP.NET MVCでURLのパラメータをControllerクラスで受け取る
バージョン
- Visual Studio 2015 Express
手順
このようなURLを開いたときに、Controllerクラスでパラメータを受け取る方法です。
http://localhost:51632/Inouts?mode=readonly&color=red
Controllerクラスを下記のように実装します。
1 2 3 4 5 6 7 8 9 10 11 12 13 |
// GET: Inouts?mode=readonly&color=red public ActionResult Index() { var requestCount = Request.QueryString.Count; Debug.WriteLine("requestCount:" + requestCount); var keys = Request.QueryString.AllKeys; foreach (var key in keys) { Debug.WriteLine("key:" + key + " value:" + Request.QueryString.Get(key)); } return View(db.inouts.ToList()); } |
Visual Studioで実行すると、出力ウィンドウにこのように表示されます。
requestCount:2
key:mode value:readonly
key:color value:red