ASP.NET MVC操作方法负责执行请求并生成对此的响应,默认情况下,它以ActionResult的形式生成响应,Action通常具有与用户交互的一对一映射。
例如,在浏览器中输入URL,单击任何特定的链接,然后提交表单,等等。这些用户交互中的每一个都会导致将请求发送到服务器,在每种情况下,请求的URL都包含MVC框架用来调用操作方法的信息,对Action方法的一个限制是它们必须是实例方法,因此它们不能是静态方法。
Action是MVC应用程序中的最终请求目的地,它使用控制器基类。让我们看一下请求处理。
链接:https://www.learnfk.comhttps://www.learnfk.com/asp.net_mvc/asp.net-mvc-actions.html
来源:LearnFk无涯教程网
当URL到达时,例如/Home/index,它是UrlRoutingModule检查并了解路由表中配置的某些内容知道如何处理该URL。
UrlRoutingModule将我们在路由表中配置的信息汇总在一起,并将控制权移交给MVC路由处理程序。
MVC路由处理程序将控制器传递给作为HTTP处理程序的MvcHandler。
MvcHandler使用控制器工厂实例化控制器,并且知道要实例化哪个控制器,因为它在RouteData中查找该控制器值。
一旦MvcHandler有了控制器,MvcHandler唯一了解的就是IController接口,因此它只是告诉控制器执行。
当它告诉控制器执行时,它是从MVC的控制器基类派生的。 Execute方法创建一个Action调用程序,并告诉该Action调用程序去调用的方法操作。
Action调用者再次在RouteData中查找并找到从路由引擎传递来的Action参数。
Action基本上会返回不同类型的Action输出, ActionResult类是所有操作输出的基础,以下是不同类型的Action输出及其行为的列表。
产品编号 | 名称和行为 |
---|---|
1 |
ContentResult 返回字符串 |
2 |
FileContentResult 返回文件内容 |
3 |
FilePathResult 返回文件内容 |
4 |
FileStreamResult 返回文件内容 |
5 |
EmptyResult 不返回任何内容 |
6 |
JavaScriptResult 返回执行脚本 |
7 |
JsonResult 返回JSON格式的数据 |
8 |
RedirectToResult 重定向到指定的URL |
9 |
HttpUnauthorizedResult 返回403 HTTP状态代码 |
10 |
RedirectToRouteResult 重定向到不同的Action/different 的控制器Action |
11 |
ViewResult 作为视图引擎的响应而收到 |
12 |
PartialViewResult 作为视图引擎的响应而收到 |
让我们看看上一章中创建了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){ var input = Server.HtmlEncode(name); return Content(input); } } }
当您请求以下URL http://localhost:61465/Employee/Mark 时,您将收到以下输出作为操作。
让我们添加另一个控制器。
步骤1 - 右键单击Controllers文件夹,然后选择Add→Controller。
它将显示"Add Scaffold"对话框。
步骤2 - 选择" MVC 5 Controller-Empty"选项,然后单击"Add"按钮。
出现“Add Controller”对话框。
步骤3 - 将名称设置为CustomerController,然后点击"Add"按钮。
现在,您将在Controllers文件夹中看到一个新的C#文件" CustomerController.cs",该文件也可以在Visual Studio中进行编辑。
同样,再添加一个名称为HomeController的控制器。以下是HomeController.cs类的实现。
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace MVCControllerDemo.Controllers { public class HomeController : Controller{ //GET: Home public string Index(){ return "This is Home Controller"; } } }
步骤4 - 运行此应用程序,您将收到以下输出。
步骤5 - 在我们上面创建的Customer控制器中添加以下代码。
public string GetAllCustomers(){ return @"<ul> <li>Ali Raza</li> <li>Mark Upston</li> <li>Allan Bommer</li> <li>Greg Jerry</li> </ul>"; }
步骤6 - 运行此应用程序,并请求 http://localhost:61465/Customer/GetAllCustomers 。您将看到以下输出。
您还可以重定向到同一控制器甚至其他控制器的操作。
以下是一个简单的示例,其中我们将通过使用以下代码更改HomeController中的代码来从HomeController重定向到Customer Controller。
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace MVCControllerDemo.Controllers{ public class HomeController : Controller{ //GET: Home public ActionResult Index(){ return RedirectToAction("GetAllCustomers","Customer"); } } }
如您所见,我们使用了RedirectToAction()方法ActionResult,该方法带有两个参数,Action名称和控制器名称。
运行此应用程序时,您会看到默认路由会将其重定向到/Customer/GetAllCustomers
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)