ASP.NET MVC - 选择器



操作选择器是可应用于操作方法的属性,用于影响响应请求时调用哪个操作方法。它帮助路由引擎选择正确的操作方法来处理特定请求。

在编写操作方法时,它起着非常关键的作用。这些选择器将根据操作方法前面修改后的名称决定方法调用的行为。它通常用于为操作方法的名称设置别名。

操作选择器属性有三种类型:

  • ActionName
  • NonAction
  • ActionVerbs

ActionName

此类表示用于操作名称的属性。它还允许开发人员使用与方法名称不同的操作名称。

让我们来看一下上一章中的一个简单示例,其中我们有包含两个操作方法的 HomeController。

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 **https://127.0.0.1:62833/Home/CurrentTime**,您将收到以下输出。

Localhost CurrentTime

您可以看到,我们在上面的 URL 中使用了 CurrentTime,而不是原始操作名称 GetCurrentTime。

NonAction

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 **https://127.0.0.1:62833/Home/CurrentTime**。您将收到以下输出。

Localhost Home CurrentTime

现在让我们检查 URL 中的 ** /TimeString** 作为操作,看看会发生什么。

TimeString

您可以看到它给出了“404—未找到”错误。

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 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);
      }
   }
}

运行此应用程序时,您将收到以下输出。

Another Search Action
广告