動くコード図鑑技術記事現場の渡り方キャリア論すべての記事About
C#

定石3:ルート制約({id:int}等)で型違いを弾く

出典: ASP.NET MVC 5 のルーティングを WinForms の Form 切替で理解する定石3:ルート制約({id:int}等)で型違いを弾く

定石3:ルート制約({id:int}等)で型違いを弾く (csharp)#530513907016
// ✅定石3:ルート制約で型違いを弾く
public class OrderController : Controller
{
    [Route("order/{id:int}")]   // idはintのみ受け付ける
    public ActionResult Detail(int id){ /* ... */ }
 
    [Route("order/code/{code:alpha:length(8)}")]   //英字8桁のcodeのみ
    public ActionResult ByCode(string code){ /* ... */ }
 
    [Route("order/{guid:guid}")]   // GUID形式のみ
    public ActionResult ByGuid(Guid guid){ /* ... */ }
 
    [Route("order/{date:datetime}")]   //日付形式のみ
    public ActionResult ByDate(DateTime date){ /* ... */ }
}
▸ この snippet は実行結果未収録
▸ 実行結果は未収録です
  • id: #530513907016
  • lines: 15
  • extracted: 2026-06-10

Source収録記事

この snippet は記事の「定石3:ルート制約({id:int}等)で型違いを弾く」セクションに登場する。コードの前後の文脈・ハマりどころの解説は記事本文で。

同じ記事から

7
図鑑トップ