简体   繁体   中英

extend ASP.NET MVC action method,How to do return View?

old:

public class HomeController : Controller {
    public ActionResult Index()
    { 
        // do something 
          return View();
    }
}  

I want extend "Index()" :

public static class HomeControllerExtensions{
   public static ActionResult Index(this HomeController hc,string viewName)
   { 
    // do something 
    return View(viewName);//hc.View cannot...., how to do return View()?
}}   

how to do return View()?

In order to be exposed to the universe as an action, a method must meet certain requirements:

  • The method must be public.
  • The method cannot be a static method.
  • The method cannot be an extension method.
  • The method cannot be a constructor, getter, or setter.
  • The method cannot have open generic types.
  • The method is not a method of the controller base class.
  • The method cannot contain ref or out parameters.

So the method can not be used as an action.

But if it is anextension method that will not be an action, you can use your hc parameter to access to methods of Controller like View() , View(string) , ...

As an alternative you may consider adding a base controller class to your project that all of your controllers can inherit from it, and in your base controller class you can add your custom action methods, override some methods of controller and so on.

What you want to do is not common. If you write more about what you want exactly, we can help better. Below is what you're trying to do. A view is invoked by System.Web.Mvc.Html.Action. That is, if the code below is useful for something, can only be used in a controller. In the example I'm in the SomeController calling the action "About" the controller "Home" using the extension you want to create.

The Extension Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using System.Web.Routing;

namespace StackOverflow.Controllers
{
    public static class ControllersExtensions
    {
        public static ActionResult Index(this HomeController controller, string ViewName)
        {
            string controllerName = controller.GetType().Name.Replace("Controller", "");
            RouteValueDictionary route = new RouteValueDictionary(new
            {
                action = ViewName,
                controller = controllerName
            });

            RedirectToRouteResult ret = new RedirectToRouteResult(route);

            return ret;
        }
    }
}

The SomeController:

namespace StackOverflow.Controllers
{
    public class SomeController : Controller
    {
        //
        // GET: /Some/

        public ActionResult Index()
        {
            HomeController home = new HomeController();

            return home.Index("About");
        }

    }
}

The Home Controller:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";

        return View();
    }

    public ActionResult About()
    {
        ViewBag.Message = "Your app description page.";

        return View();
    }

    public ActionResult Contact()
    {
        ViewBag.Message = "Your contact page.";

        return View();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM