C#
定石1:デフォルトルート(Convention-based)の仕組み
出典: ASP.NET MVC 5 のルーティングを WinForms の Form 切替で理解する — 定石1:デフォルトルート(Convention-based)の仕組み
// ✅定石1:デフォルトルート({controller}/{action}/{id})
using System.Web.Mvc;
using System.Web.Routing;
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
▸ この snippet は実行結果未収録
▸ 実行結果は未収録です
Source収録記事
この snippet は記事の「定石1:デフォルトルート(Convention-based)の仕組み」セクションに登場する。コードの前後の文脈・ハマりどころの解説は記事本文で。
同じ記事から
7 件// ✅定石2:属性ルート([Route] でActionに直接指定) [RoutePrefix("customer")] // Controller全体のプレフィックス public class CustomerController : Controller {未収録
定石2:属性ルート(Attribute-based)— URLを綺麗にしたい時
#949a0d365263
public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}");未収録
定石2:属性ルート(Attribute-based)— URLを綺麗にしたい時
#7a3019f8a2f6
// ✅定石3:ルート制約で型違いを弾く public class OrderController : Controller { [Route("order/{id:int}")] // idはintのみ受け付ける未収録
定石3:ルート制約({id:int}等)で型違いを弾く
#530513907016
// ✅定石4:カスタム階層ルート(API風) [RoutePrefix("api/v1")] public class OrdersApiController : Controller {未収録
定石4:カスタム階層ルート— REST風APIのような綺麗なURL
#93ae011bd8fb
// ✅ WinForms版: Formを切り替える public partial class CustomerListForm : Form { private void btnNewCustomer_Click(object sender, EventArgs e)未収録
定石5: Form切替↔ RedirectToActionの対比
#b65535aa934a
// ✅ ASP.NET MVC版: Actionにリダイレクト public class CustomerController : Controller { public ActionResult Index(){ /* 一覧表示 */ }未収録
定石5: Form切替↔ RedirectToActionの対比
#7ae814a5608b