ActionInvoker is used to stimulate the response action based on the request data (HttpPost, HttpGet, etc.) and action name, and then render the view by action. This article uses custom ActionInvoker to render the view based on the request type.
The Controller has such an Action:
Public ActionResult Demo ()
{
Return View ();
}
When requesting this action, we hope that: if it is a Get request, we will directly render the view DemoGet. cshtml; if it is a Post request, we will directly render the view DemoPost. cshtml. Therefore, we need to extend the default ControllerActionInvoker class.
using System.Web.Mvc;
namespace MvcApplication1.Extension
{
public class MyActionInvoker : ControllerActionInvoker
{
public override bool InvokeAction(ControllerContext controllerContext, string actionName)
{
if (controllerContext.HttpContext.Request.RequestType == "GET" && actionName == "Demo")
{
ViewResult viewResult = new ViewResult();
viewResult.View = viewResult.ViewEngineCollection.FindView(controllerContext, "DemoGet", null).View;
InvokeActionResult(controllerContext, viewResult);
return true;
}
else if (controllerContext.HttpContext.Request.RequestType == "POST" && actionName == "Demo")
{
ViewResult viewResult = new ViewResult();
viewResult.View = viewResult.ViewEngineCollection.FindView(controllerContext, "DemoPost", null).View;
InvokeActionResult(controllerContext, viewResult);
return true;
}
else
{
return base.InvokeAction(controllerContext, actionName);
}
}
}
}
Enable custom ActionInvoker in the Controller constructor.
public HomeController()
{
this.ActionInvoker = new MyActionInvoker();
}
DemoGet. cshtml View:
@{
ViewBag.Title = "DemoGet";
Layout = "~/Views/Shared/_Layout.cshtml";
}
@using (Html.BeginForm("Demo", "Home", FormMethod.Post, new {id = "form1"}))
{
<input type="text" name="name" id="name"/>
<Input type = "submit" value = "submit" name = "submit" id = "submit"/>
}
@section scripts
{
<script type="text/javascript">
$(function() {
$('#submit').click(function() {
var name = $('#name').val();
var url = $('#form1').attr('action');
url = url + '?name=' + name;
$('#form1').attr('action', url);
});
});
</script>
}
DemoPost. cshtml View:
@{
ViewBag.Title = "DemoPost";
Layout = "~/Views/Shared/_Layout.cshtml";
}
@{ string name = string.Empty;}
@if (HttpContext.Current.Request.QueryString["name"] != null)
{
name = HttpContext.Current.Request.QueryString["name"].ToString();
}
@name
When entering:/Home/Demo in the browser, It is a Get request and the DemoGet. cshtml view is returned:
When the submit button is clicked, the Post request is sent to/Home/Demo, and the DemoPost. cshtml view is returned:
Summary:
For a controller method, it can be based on the request data (may be GET, POST, or other request data, because it can be from HttpContext. current. different Request data), directly rendering the view. You may not use the following method:
[HttpGet]
Public ActionResult SomeAction ()
[HttpPost]
Public ActionResult SomeAction ()
References:
Controller Factory and Action Invoker Part 2