首页 > 编程语言 >无涯教程-ASP.NET MVC - 选择器

无涯教程-ASP.NET MVC - 选择器

时间:2023-10-12 13:32:23浏览次数:32  
标签:Web ASP return 无涯 System 选择器 input using public

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

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

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

参考链接

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

标签:Web,ASP,return,无涯,System,选择器,input,using,public
From: https://blog.51cto.com/u_14033984/7826724

相关文章

  • 无涯教程-ASP.NET MVC - 控制器
    控制器本质上是ASP.NETMVC应用程序的中央单元,控制器决定将选择哪个模型,然后在呈现该视图之后,从模型中获取数据并将其传递给相应的视图。控制器是从System.Web.Mvc.Controller继承的C#类,System.Web.Mvc.Controller是内置的控制器基类,控制器中的每个公共方法都称为操作方法,这意味......
  • 无涯教程-ASP.NET MVC - 简介
    ASP.NETCoreMVC是ASP.NETCore内,提供给Web应用程序开发的框架,它可视为ASP.NETMVC的后继版本,其主要功能均衍生自ASP.NETMVC,但它除了基于ASP.NETCore外,也将ASP.NETMVC与类似平台进行了整合,例如负责View的ASP.NETWebPages以及负责RESTfulAPI的ASP.NETW......
  • 无涯教程-ASP.NET Core - Razor表单
    在本章中,无涯教程将继续讨论标签助手,还将在应用程序中添加新功能,并使其能够编辑现有员工的详细信息,将在每个员工添加一个链接,该链接将转到HomeController上的Edit动作。@modelHomePageViewModel@{ViewBag.Title="Home";}<h1>Welcome!</h1><table>@forea......
  • 无涯教程-ASP.NET Core - Razor导入
    在本章中,无涯教程将讨论RazorViewImport,除了ViewStart文件之外,还有一个ViewImports文件,MVC框架在呈现任何视图时都将查找该文件。就像ViewStart文件一样,可以将ViewImports.cshtml放到文件夹中,并且ViewImports文件可以影响文件夹层次结构中的所有视图。此视图是此版本MVC的......
  • 无涯教程-ASP.NET Core - Razor视图
    在本章中,无涯教程将讨论RazorViewStart。MVC中的Razor视图引擎有一个约定,即它将查找名称为_ViewStart.cshtml的任何文件,并在该文件中执行代码。ViewStart文件中的代码无法呈现到页面的HTML输出中,但可用于从各个视图内的代码块中删除重复的代码。在示例中,如果希望每个视图......
  • 无涯教程-ASP.NET Core - Razor布局
    在本章中,无涯教程将了解“RazorLayout”视图,大多数网站和Web应用程序都希望创建呈现一些常见元素的页面。布局视图现在了解什么是布局视图。"Layout"视图是扩展名为*.cshtml的Razor视图,您可以选择以所需的方式命名布局视图,在本章中,将使用名为_Layout.cshtml。这是"Layou......
  • 无涯教程-ASP.NET Core - 实体框架
    在本章中,无涯教程将设置和配置应用程序以保存和读取SQLServer数据库中的数据。要使用数据库,将使用实体框架,该框架经过了重新编写以与新的.NETFramework一起使用。在此应用程序中,将使用SQLServerLocalDB。LocalDB是为开发人员优化的SQLServer的特殊版本。VisualStudio......
  • 无涯教程-ASP.NET Core - 属性路由
    在本章中,无涯教程将学习另一种路由方法,即基于属性的路由,通过基于属性的路由,可以在控制器类和这些类内部的方法上使用C#属性,这些属性具有告诉ASP.NETCore何时调用特定控制器的元数据。它是基于约定的路由的替代方法。按照出现的顺序,注册的顺序对路由进行判断,但是映射多个路由是......
  • 无涯教程-ASP.NET Core - MVC设置
    在本章中,无涯教程将在FirstAppDemo应用程序设置为MVC框架,将在ASP.NETCore(更具体地说,ASP.NETCoreMVC框架)构建一个Web应用程序,从技术上讲,只能使用中间件来构建整个应用程序,但是ASP.NETCoreMVC提供了可轻松创建HTML页面和基于HTTP的API的功能。要在空项目中设置MVC框架,请遵......
  • 无涯教程-ASP.NET Core - 中间件
    在本章中,无涯教程将了解如何设置中间件(Middleware),ASP.NETCore中间件控制应用程序如何响应HTTP请求。现在假设想将有关每个请求的信息记录到应用程序中。在这种情况下,可能会安装到应用程序中的第一个中间件是日志记录(Logger)组件。该记录器(Logger)可以看到有关传入请求的......