ASP.NET MVCでコントローラ・クラスを定義するのは、さほど難しいことではない。コントローラ・クラスであることの条件は「Controllerクラスを継承していること」だけである*1。「ASP.NET MVCフレームワーク 基本のキ」では、コントローラ・クラスは「アプリケーション・ルート直下の/Controllersフォルダに配置すること」としているが、実はこれすらも必須ではない。ASP.NET MVCは、Controller派生クラスをあらゆる名前空間(現在のプロジェクトとその参照先のアセンブリ)から検索するため、特に配置先のフォルダや名前空間を制限していないのだ。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
namespace MvcApp.Controllers {
// Sample/Indexアクションを定義
public class SampleController : Controller {
public ActionResult Index() {
return Content("Controllers名前空間");
}
}
}
Namespace Controllers
' Sample/Indexアクションを定義
Public Class SampleController
Inherits System.Web.Mvc.Controller
Function Index() As ActionResult
Return Content("Controllers名前空間")
End Function
End Class
End Namespace
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
namespace MvcApp.Controllers2 {
// Sample/Indexアクションを定義
public class SampleController : Controller {
public ActionResult Index() {
return Content("Controllers2名前空間");
}
}
}
Namespace Controllers2
' Sample/Indexアクションを定義
Public Class SampleController
Inherits System.Web.Mvc.Controller
Function Index() As ActionResult
Return Content("Controllers2名前空間")
End Function
End Class
End Namespace