ベノーネ
ポイント1への答え
_ViewStart ファイルを使用して、各ビューのレンダリングの開始時に実行する共通のビュー コードを定義できます。たとえば、_ViewStart.cshtml ファイル内にコードを記述して、プログラムで各ビューの Layout プロパティを既定で SiteLayout.cshtml ファイルに設定することができます。
実際には、共通コードを保持できる ASP.Net の BasePage のようなものです。
または、以下のようにビューにロジックを直接記述できます。
@{
Layout = "~/Views/Shared/_Layout.cshtml";
if (Some Consition) {
Layout = "~/Views/Shared/_AdminLayout.cshtml";
}
}
あるいは
Action メソッドを実行する前に実行される Action Executing メソッドをオーバーライドできます。特定の Action メソッドまたは Complete Controller に対して設定できます
以下は、Complete Controller 用に設定するためのコードです。
protected override void OnActionExecuting(ActionExecutingContext ctx) {
base.OnActionExecuting(ctx);
}
以下は、特定のアクションメソッドに設定するためのコードです
[MyAttribute(SomeProperty = "")]
public ActionResult Index()
{
return View("Index");
}
public class MyAttribute : ActionFilterAttribute
{
public string SomeProperty { get; set; }
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
base.OnActionExecuting(filterContext);
}
}
ポイント2への答え
使用できます
var str = Html.Partial("_Partial_View_Name");
Partial は MvcHtmlString を返します。出力を変数に設定して必要な変更を加えることで、出力をインターセプトできます。
ポイント3への答え
はい。以下はサンプルコードです
namespace MvcApplication1.Controllers
{
[HandleError]
public class HomeController : Controller
{
[OutputCache(Duration=10, VaryByParam="none")]
public ActionResult Index()
{
return View();
}
}
}
Index() アクションの出力は 10 秒間キャッシュされます