0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Html.ActionLinkを使うとhrefに「?Length=4」がついてしまう

Posted at

起こったこと

Viewで@Html.ActionLinkを使ってaタグを作成すると、hrefに「?Length=4」がついてしまう。

CSHTML
@Html.ActionLink("About", "About", "Home", new { @class = "btn btn-primary" })

原因

コントローラーを直で書いているのが原因みたい。

string routeValuesのオブジェクト「Home」を取得します。MVCはこれをパブリックプロパティを検索してルート値に変換します。stringオブジェクトの場合、唯一のパブリックプロパティはLengthであり、Lengthパラメータでルートが定義されないため、プロパティ名と値がクエリ文字列パラメータとして追加されます。

対処

string controllerNameにべた書きせずにstring routeValuesに設定する。

CSHTML
@Html.ActionLink("About", "About", new { controller = "Home" }, new { @class = "btn btn-primary" })
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?