Asp.net-mvc-selectors

提供:Dev Guides
移動先:案内検索

ASP.NET MVC-セレクター

アクションセレクターは、アクションメソッドに適用できる属性であり、要求に応じてどのアクションメソッドが呼び出されるかに影響を与えるために使用されます。 ルーティングエンジンが特定のリクエストを処理するための正しいアクションメソッドを選択するのに役立ちます。

アクションメソッドを記述するときに非常に重要な役割を果たします。 これらのセレクターは、アクションメソッドの前に指定された変更名に基づいて、メソッド呼び出しの動作を決定します。 通常、アクションメソッドの名前のエイリアスとして使用されます。

アクションセレクター属性には3種類あります-

  • ActionName
  • 不行動
  • 他動詞

ActionName

このクラスは、アクションの名前に使用される属性を表します。 また、開発者はメソッド名とは異なるアクション名を使用できます。

HomeControllerに2つのアクションメソッドが含まれている最後の章の簡単な例を見てみましょう。

using System;
using System.Collections.Generic;
using System.Linq;

using System.Web;
using System.Web.Mvc;

namespace MVCFiltersDemo.Controllers {
   public class HomeController : Controller{
     //GET: Home
      public string Index(){
         return "This is ASP.Net MVC Filters Tutorial";
      }

      public string GetCurrentTime(){
         return DateTime.Now.ToString("T");
      }
   }
}

次のコードに示すように、GetCurrentTime()の上に[ActionName( "CurrentTime")]を記述して、GetCurrentTimeのActionNameセレクターを適用しましょう。

using System;
using System.Collections.Generic;
using System.Linq;

using System.Web;
using System.Web.Mvc;

namespace MVCFiltersDemo.Controllers {
   public class HomeController : Controller{
     //GET: Home
      public string Index(){
         return "This is ASP.Net MVC Filters Tutorial";
      }

      [ActionName("CurrentTime")]
      public string GetCurrentTime(){
         return DateTime.Now.ToString("T");
      }
   }
}

このアプリケーションを実行し、ブラウザに次のURLを入力します http://localhost:62833/Home/CurrentTime 、次の出力を受け取ります。

Localhost CurrentTime

上記のURLの元のアクション名であるGetCurrentTimeの代わりにCurrentTimeを使用したことがわかります。

不行動

NonActionは別の組み込み属性であり、コントローラーのパブリックメソッドがアクションメソッドではないことを示します。 メソッドをアクションメソッドとして扱わないようにする場合に使用します。

HomeControllerに別のメソッドを追加して簡単な例を見てみましょう。また、次のコードを使用してNonAction属性を適用します。

using MVCFiltersDemo.ActionFilters;
using System;
using System.Collections.Generic;
using System.Linq;

using System.Web;
using System.Web.Mvc;

namespace MVCFiltersDemo.Controllers {
   public class HomeController : Controller{
     //GET: Home
      public string Index(){
         return "This is ASP.Net MVC Filters Tutorial";
      }

      [ActionName("CurrentTime")]
      public string GetCurrentTime(){
         return TimeString();
      }

      [NonAction]
      public string TimeString(){
         return "Time is " + DateTime.Now.ToString("T");
      }
   }
}

新しいメソッドTimeStringはGetCurrentTime()から呼び出されますが、URLのアクションとして使用することはできません。

このアプリケーションを実行して、ブラウザーで次のURL http://localhost:62833/Home/CurrentTime を指定してみましょう。 次の出力が表示されます。

Localhost Home CurrentTime

次に、URLのアクションとして /TimeString を確認し、何が起こるかを見てみましょう。

TimeString

「404—Not Found」エラーが発生することがわかります。

他動詞

適用できる別のセレクタフィルタは、ActionVerbs属性です。 そのため、特定のアクションの表示を特定のHttpVerbsに制限します。 同じ名前の2つの異なるアクションメソッドを定義できますが、1つのアクションメソッドはHTTP Get要求に応答し、別のアクションメソッドはHTTP Post要求に応答します。

MVCフレームワークは、次のActionVerbsをサポートしています。

  • HttpGet
  • HttpPost
  • HttpPut
  • HttpDelete
  • HttpOptions
  • HttpPatch

EmployeeControllerを作成する簡単な例を見てみましょう。

using System;
using System.Collections.Generic;
using System.Linq;

using System.Web;
using System.Web.Mvc;

namespace MVCControllerDemo.Controllers {
   public class EmployeeController : Controller{
     //GET: Employee
      public ActionResult Search(string name = “No name Entered”){
         var input = Server.HtmlEncode(name);
         return Content(input);
      }
   }
}

次のコードを使用して、同じ名前の別のアクションメソッドを追加しましょう。

using System;
using System.Collections.Generic;
using System.Linq;

using System.Web;
using System.Web.Mvc;

namespace MVCControllerDemo.Controllers {
   public class EmployeeController : Controller{
     //GET: Employee
     //public ActionResult Index()
     //{
        //return View();
     //}

      public ActionResult Search(string name){
         var input = Server.HtmlEncode(name);
         return Content(input);
      }

      public ActionResult Search(){
         var input = "Another Search action";
         return Content(input);
      }
   }
}

このアプリケーションを実行すると、MVCフレームワークがリクエストに対してどのアクションメソッドをピックアップする必要があるかを判断できないため、エラーが発生します。

次のコードを使用して、応答として必要なアクションでHttpGet ActionVerbを指定しましょう。

using System;
using System.Collections.Generic;
using System.Linq;

using System.Web;
using System.Web.Mvc;

namespace MVCControllerDemo.Controllers {
   public class EmployeeController : Controller{
     //GET: Employee
     //public ActionResult Index()
     //{
        //return View();
     //}

      public ActionResult Search(string name){
         var input = Server.HtmlEncode(name);
         return Content(input);
      }

      [HttpGet]
      public ActionResult Search(){
         var input = "Another Search action";
         return Content(input);
      }
   }
}

このアプリケーションを実行すると、次の出力が表示されます。

別の検索アクション