1
0

More than 1 year has passed since last update.

【ASP.NET Core MVC】URLにパラメータを指定してもコントローラー側で値を取得できない

Last updated at Posted at 2022-08-11

環境

ASP.NET MVC
.NET Core 3.1

概況

ASP.NETを勉強し始めたばかり(web系も初めて)の初心者です。
備忘録のために残しています。

状況

ルーティングは以下の通りでした。(デフォルトのまま)

startup.cs
app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=Home}/{action=Index}/{id?}");
            });

パラメータが受け取れなかったStudentControllerのIndexは以下の通りです。

StudentController.cs
public ActionResult Index(int biPage)
{       
    //中身は省略
    return View();
}

原因

Indexメソッドの引数をルーティングで指定した「id」という名前にしていなかったからでした。
(ページを指定していたのでbiPageという名前にしていた。)

引数の数が合っていれば名前は変更しても大丈夫だと思い込んでいたので、
かなり悩んで時間を使ってしまいました。。。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0