動(dòng)作選擇器是可以應(yīng)用于動(dòng)作方法的屬性,用于影響響應(yīng)請求時(shí)調(diào)用哪個(gè)動(dòng)作方法。 它有助于路由引擎選擇正確的操作方法來處理特定的請求。
當(dāng)你編寫動(dòng)作方法時(shí),它扮演著非常關(guān)鍵的角色。 這些選擇器將根據(jù)動(dòng)作方法前面給出的修改后的名稱來決定方法調(diào)用的行為。動(dòng)作選擇器通常用于別名操作方法的名稱。
動(dòng)作選擇器有三種類型的屬性 -
這個(gè)類表示一個(gè)用于動(dòng)作名稱的屬性。它還允許開發(fā)人員使用與方法名稱不同的動(dòng)作名稱(即動(dòng)作的別名)。
我們來創(chuàng)建一個(gè)項(xiàng)目:MVCSelectors,在這個(gè)項(xiàng)目中,創(chuàng)建一個(gè)控制器 - HomeController ,它包含有兩個(gè)操作方法。參考以下代碼 -
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")]
來應(yīng)用GetCurrentTime
的ActionName
選擇器,如以下代碼所示 -
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");
}
}
}
現(xiàn)在運(yùn)行該應(yīng)用程序并在瀏覽器URL欄中輸入以下URL:http://localhost:62833/Home/CurrentTime
(不是GetCurrentTime
),將收到以下輸出結(jié)果 -
可以看到已經(jīng)使用了CurrentTime
,而不是原來的動(dòng)作名稱,在上面正常的URL應(yīng)該是:GetCurrentTime
。
NonAction
是另一個(gè)內(nèi)置屬性,它表示Controller
的公共方法不是一個(gè)操作方法。當(dāng)想要一個(gè)方法不要被當(dāng)作一個(gè)操作方法來處理的時(shí)候,就可以使用它了。
下面來看看一個(gè)簡單的例子,在HomeController
中添加另一個(gè)方法,并使用下面的代碼應(yīng)用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 "當(dāng)前時(shí)間是: " + DateTime.Now.ToString("T");
}
}
}
新的方法TimeString()
是要被GetCurrentTime()
方法調(diào)用的,但不能在URL中作為動(dòng)作使用。
運(yùn)行這個(gè)應(yīng)用程序,并在瀏覽器中指定以下URL: http://localhost:62466/Home/CurrentTime
, 將收到以下輸出。如下圖所示 -
現(xiàn)在來看看在URL中使用/Home/TimeString
作為動(dòng)作,看看會(huì)發(fā)生什么。如下圖所示 -
可以看到它給出了“404-找不到” 的錯(cuò)誤。
另一個(gè)可以應(yīng)用的選擇器過濾器是ActionVerbs
屬性。所以這限制了特定行為對特定的HttpVerbs
的指示。可以定義兩個(gè)具有相同名稱的不同操作方法,但是一個(gè)操作方法會(huì)響應(yīng)HTTP Get請求,另一個(gè)操作方法會(huì)響應(yīng)HTTP Post請求。
MVC框架支持以下ActionVerbs
-
下面來看一個(gè)簡單的例子,創(chuàng)建一個(gè)控制器 - 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);
}
}
}
現(xiàn)在使用下面的代碼添加另一個(gè)具有相同名稱的操作方法。
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);
}
}
}
當(dāng)運(yùn)行這個(gè)應(yīng)用程序時(shí),它會(huì)給出一個(gè)錯(cuò)誤,因?yàn)镸VC框架無法確定應(yīng)該為請求選擇哪個(gè)操作方法。
使用下面的代碼來指定HttpGet ActionVerb
作為響應(yīng)的動(dòng)作。
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);
}
}
}
當(dāng)運(yùn)行此應(yīng)用程序時(shí),將收到以下輸出結(jié)果 -