ASP.NET MVC - 选择器

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

在编写动作方法时,它起着非常重要的作用.这些选择器将根据action方法前面给出的修改名称来决定方法调用的行为.它通常用于别名操作方法的名称.

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

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

让我们通过编写[ActionName("CurrentTime")来为GetCurrentTime应用ActionName选择器在GetCurrentTime()之上,如下面的代码所示.

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

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

NonAction

NonAction是另一个内置属性,表示Controller的公共方法不是动作方法.当你想要一个方法不应该被视为一个动作方法时使用它.

让我们看一个简单的例子,在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");
      }
   }
}
从GetCurrentTime()调用新方法TimeString但是你不能使用它作为URL中的操作.

让我们运行此应用程序并在浏览器中指定以下URL http://localhost:62833/Home/CurrentTime .您将收到以下输出.

Localhost Home CurrentTime

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

TimeString

你可以看到它给出了'404-Not Found'错误.

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