MVC extension ActionInvoker, custom ActionInvoker, return different views based on request data

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.