ASP.Net MVC選擇器

動作選擇器是可以應用於動作方法的屬性,用於影響響應請求時調用哪個動作方法。 它有助於路由引擎選擇正確的操作方法來處理特定的請求。

當你編寫動作方法時,它扮演着非常關鍵的角色。 這些選擇器將根據動作方法前面給出的修改後的名稱來決定方法調用的行爲。動作選擇器通常用於別名操作方法的名稱。

動作選擇器有三種類型的屬性 -

  • ActionName
  • NonAction
  • ActionVerbs

1. ActionName

這個類表示一個用於動作名稱的屬性。它還允許開發人員使用與方法名稱不同的動作名稱(即動作的別名)。

我們來創建一個項目:MVCSelectors,在這個項目中,創建一個控制器 - HomeController ,它包含有兩個操作方法。參考以下代碼 -

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MVCSelectors.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("yyyy-MM-dd HH:mm:ss");
        }
    }
}

通過在GetCurrentTime()方法之上寫[ActionName("CurrentTime")]來應用GetCurrentTimeActionName選擇器,如以下代碼所示 -

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MVCSelectors.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("yyyy-MM-dd HH:mm:ss");
        }
    }
}

現在運行該應用程序並在瀏覽器URL欄中輸入以下URL:http://localhost:62833/Home/CurrentTime(不是GetCurrentTime),將收到以下輸出結果 -

ASP.Net

可以看到已經使用了CurrentTime,而不是原來的動作名稱,在上面正常的URL應該是:GetCurrentTime

2. NonAction

NonAction是另一個內置屬性,它表示Controller的公共方法不是一個操作方法。當想要一個方法不要被當作一個操作方法來處理的時候,就可以使用它了。

下面來看看一個簡單的例子,在HomeController中添加另一個方法,並使用下面的代碼應用NonAction屬性。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MVCSelectors.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 "當前時間是: " + DateTime.Now.ToString("T");
        }
    }
}

新的方法TimeString()是要被GetCurrentTime()方法調用的,但不能在URL中作爲動作使用。

運行這個應用程序,並在瀏覽器中指定以下URL: http://localhost:62466/Home/CurrentTime , 將收到以下輸出。如下圖所示 -
ASP.Net

現在來看看在URL中使用/Home/TimeString作爲動作,看看會發生什麼。如下圖所示 -
ASP.Net

可以看到它給出了「404-找不到」 的錯誤。

3. ActionVerbs

另一個可以應用的選擇器過濾器是ActionVerbs屬性。所以這限制了特定行爲對特定的HttpVerbs的指示。可以定義兩個具有相同名稱的不同操作方法,但是一個操作方法會響應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 MVCSelectors.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 MVCSelectors.Controllers
{
    public class EmployeeController : Controller
    {
        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框架無法確定應該爲請求選擇哪個操作方法。

ASP.Net

使用下面的代碼來指定HttpGet ActionVerb作爲響應的動作。

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

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


namespace MVCSelectors.Controllers
{
    public class EmployeeController : Controller
    {
        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);
        }
    }
}

當運行此應用程序時,將收到以下輸出結果 -

ASP.Net