ASP.NET MVC - 选择器

ASP.NET MVC - 选择器 首页 / ASP.Net MVC入门教程 / ASP.NET MVC - 选择器

Action Selector是可以应用于Action方法的属性,用于响应请求而调用哪种Action方法,它有助于路由引擎选择正确的操作方法来处理特定请求。

在编写Action方法时,它起着至关重要的作用。这些选择器将根据操作方法来决定方法调用的行为,它通常用于为操作方法的名称加上别名。

Action Selector选择器属性有三种类型-

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

现在运行此应用程序,并在浏览器 http://localhost:62833/Home/CurrentTime 中输入以下URL,您将收到以下输出。

Localhost CurrentTime

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

NonAction

NonAction是另一个内置属性,它指示Controller的公共方法不是操作方法,当您不希望将某个方法视为Action方法时,可以使用该方法。

让我们看一个简单的示例,在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

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

TimeString

您会看到它显示" 404-Not Found"错误。

ActionVerbs

您可以应用的另一个选择器过滤器是ActionVerbs属性,因此,这将对特定操作的指示限制为特定的HttpVerbs,您可以定义两个具有相同名称的不同操作方法,但是一个操作方法将响应HTTP Get请求,而另一个操作方法将响应HTTP Post请求。

无涯教程网

MVC框架支持以下ActionVerb。

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

现在,使用以下代码添加另一个具有相同名称的操作方法。

链接:https://www.learnfk.comhttps://www.learnfk.com/asp.net_mvc/asp.net-mvc-selectors.html

来源:LearnFk无涯教程网

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以及您想要作为响应的Action。

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

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

零基础学Java -〔臧萌〕

OpenResty从入门到实战 -〔温铭〕

移动端自动化测试实战 -〔思寒〕

架构实战案例解析 -〔王庆友〕

软件设计之美 -〔郑晔〕

乔新亮的CTO成长复盘 -〔乔新亮〕

中间件核心技术与实战 -〔丁威〕

LangChain 实战课 -〔黄佳〕

程序员职业规划手册 -〔雪梅〕

好记忆不如烂笔头。留下您的足迹吧 :)