ASP.NET MVC 3 中的 Actions
深入探讨 Controller 中使用的不同 Actions、Action 过滤器和选择器。
引言
ASP.NET MVC 提供了一种创建 Web 应用程序的新方法,这种方法更具可扩展性和可测试性。我们已经在ASP.NET MVC 3 简介中讨论了 ASP.NET MVC。此外,我们还讨论了ASP.NET MVC 3 中的路由器和控制器。在这里,我们将深入研究 Controller 中使用的不同 Actions、Action 过滤器和选择器。
操作结果
默认情况下,Controller actions 将返回 ActionResult
对象。我们可以返回各种类型的 ActionResult
结果,这将决定如何在浏览器上呈现输出。
public ActionResult About()
{
return View();
}
示例 Controller
对于我们的示例,除了默认的 HomeController
和 AccountController
之外,我们将使用以下 SampleController
。
public class SampleController : Controller
{
//
// GET: /Sample/
public ActionResult Index()
{
return Content("Hello from Index action in Sample Controller");
}
public ActionResult Verify(string username = "all")
{
return Content("Hello from Verify action in Sample Controller");
}
}
在这里,我们将讨论作为 ASP.NET MVC 3 一部分的某些 ActionResult
。
1. Content
当我们需要从 Controller action 返回任何文本时,我们将使用 Content
类型。
public ActionResult Index()
{
return Content("Hello from Home Controller");
}
2. RedirectToAction
根据输入值,我们可以重定向到另一个 Action。为了重定向到另一个 Action,我们将使用 RedirectToAction
类型。
public ActionResult Index()
{
// Redirect to Verify action inside the Sample Controller
return RedirectToAction("Verify", "Sample");
}
3. RedirectToRoute
当我们需要重定向到在 Global.asax 中定义的路由时,我们将使用 RedirectToRoute
对象。
作为我们示例应用程序的一部分,我们定义了一个名为“sample
”的自定义路由。这将路由到 Sample Controller 中的 Index action。有关自定义路由的更多信息,请参阅ASP.NET MVC 3 中的控制器和路由器。
public ActionResult Index()
{
return RedirectToRoute("sample");
}
4. File
File
用于将文件的内容返回到浏览器。对于我们的示例,我将 web.config 返回到浏览器。
public ActionResult Index()
{
return File("Web.config", "text/html");
}
5. JSON
我们可以将文本呈现到结果页面,或者使用 JSON 格式将其作为文件发送给客户端。
public ActionResult Index()
{
return Json("hello from JSON","text/html", JsonRequestBehavior.AllowGet);
}
正如我们指定的内容类型一样,它将在浏览器中呈现,如下所示
public ActionResult Index()
{
return Json("hello from JSON", JsonRequestBehavior.AllowGet);
}
如果没有指定内容类型,它将下载内容作为文件。
操作过滤器
ASP.NET MVC 3 提供了 一组 Action 过滤器来过滤 actions。 Action 过滤器被定义为属性,并应用于 Action 或控制器。
1. Authorize
Authorize 过滤器确保只有授权用户才能调用相应的 Action。如果用户未授权,他将被重定向到登录页面。
[Authorize]
public ActionResult About()
{
return View();
}
如果用户未授权并调用 About action,则他将被重定向到登录页面。
如果我们需要在控制器级别使用过滤器,那么我们可以将过滤器添加到控制器类本身。
[Authorize]
public class SampleController : Controller
{
……………………………………………
}
2. HandleError
HandleError
将处理应用程序抛出的各种异常,并向用户显示用户友好的消息。默认情况下,此过滤器在 Global.asax 中注册。
注意:如果任何 Action 过滤器被添加到 Global.asax 中的过滤器集合中,那么过滤器的范围就是应用程序。
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
}
为了验证过滤器,让我们在 web.config 中启用自定义错误
<customErrors mode="On"/>
现在从 Home Controller 抛出异常
public ActionResult Index()
{
throw new Exception("Verify the HandleError filter");
ViewBag.Message = "Welcome to ASP.NET MVC!";
return View();
}
现在运行应用程序并观察自定义错误消息
此 View 在 Shared Views 下定义。我们可以使用 Error.cshtml 文件更改错误消息。
现在,让我们从 Global.asax 中删除过滤器并验证我们的应用程序。我们将收到以下服务器错误
现在,让我们将 CustomError
标签更新为 RemoteOnly
并验证我们的应用程序。现在我们可以看到由应用程序抛出的此错误消息
我们可以为 Action 或控制器指定 HandleError
过滤器。
ASP.NET MVC 将 Action 过滤器(如 OutputCache
、ValidateInput
等)定义为 ASP.NET MVC 3 的一部分,我们将在稍后讨论。
Action 选择器
ASP.NET MVC 3 定义了一组 Action 选择器,它们确定 Action 的选择。其中之一是 ActionName
,用于为 Action 定义别名。当我们为 Action 定义别名时,Action 将仅使用别名调用;不是使用 Action 名称。
[ActionName("NewAbout")]
public ActionResult About()
{
return Content("Hello from New About");
}
ASP.NET 还有更多 Action 选择器,例如 HTTPPost
和 HTTPGet
,我们将在稍后讨论。
结论
在这里,我们快速讨论了各种 Action 结果选项和 Action 过滤器。我们将在下一篇文章中讨论有关 Views、Styles 等的更多内容。