ASP.NET Coreのルーティング 2.1 -> 2.2
基本的にわたしがやらかしたことを晒している本ブログですがまたやらかしたので報告します
ASP.NET Core 2.2がリリースされて日本のリージョンにも入ったので2.2を使いましょー
とりあえず準備はVisual Studio 2017で新規のASP.NET Core 2.2を作成
2.1のルーティングを確かめる
2.2で作りましたがまずは2.1のころの動作をチェックします
以下変更点だけ
Startup.cs
public void ConfigureServices(IServiceCollection services) { services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1); }
HomeController.cx
public class HomeController : Controller { public IActionResult Index() => View(); [Route("[controller]/test/{id}")] public IActionResult Test(string id) => Ok(); }
Index.cshtml
@Context.Request.Host@Context.Request.Path <a asp-controller="Home" asp-action="Test">test</a>
出力
localhost:44379/ <a href="/Home/Test">test</a>
2.2で動かしてみる
Startup.cs
public void ConfigureServices(IServiceCollection services) { services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2); }
CompatibilityVersion.Version_2_2
に変更するだけ
出力
localhost:44379/ <a href="">test</a>
hrefの中身が空になりました
変更点
ここにいろいろ書いてありますが
IRouter ベースのルーティングでは、BlogController が存在しない場合や ReadPost アクション メソッドがない場合でも、結果は常に /Blog/ReadPost/17 となります。 予想どおり、アクション メソッドが存在する場合は、ASP.NET Core 2.2 以降のエンドポイント ルーティングで /Blog/ReadPost/17 が生成されます。 しかし、アクションが存在しない場合は、エンドポイント ルーティングで空の文字列が生成されます。
Index.cshtmlのアンカータグヘルパーで書いたところ<a asp-controller="Home" asp-action="Test">test</a>
はid
の指定がないのですが、2.1のころはあるものとしてリンクを生成して、2.2以降は無いからnullにする、ってことですね
idの指定をJavaScriptでしたいのでその前までのURLが欲しい!って思って使ってた私には効果が絶大でした
おまけ
なんとか致命傷で済んだのですが、ルーティングでもう一つやらかしてました
まずHomeController.csに少し手を加えます
HomeController.cx
public class HomeController : Controller { public IActionResult Index() => View(); [Route("[controller]/index/{id}")] public IActionResult Index(string id) => View(); [Route("[controller]/test/{id}")] public IActionResult Test(string id) => Ok(); }
んでlocalhost:44379/home/index/1
にアクセスすると
- 2.1
localhost:44379/home/index/1 <a href="/Home/test/1">test</a>
- 2.2
localhost:44379/home/index/1 <a href="">test</a>
これは2.1の動作が謎っぽいんですが、idが勝手に付与されてリンク生成するんですね
そしてそういうものだと思って使っていたので無事死亡しました
まとめ
例ではアンカータグヘルパーで試しましたがUrl.Action()
なんかでも同じだよ!
CompatibilityVersion.Version_2_2
の指定は計画的に
2018/12/27 追記
しばやん先生が分かりやすく解説してくれてます