0

Old:

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

I want to 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 return View()?

Amal K
  • 4,359
  • 2
  • 22
  • 44
Tomcat
  • 13
  • 1
  • 3
  • 3
    what are you actually trying to do here? I don't see a use-case. – Jonesopolis Sep 11 '15 at 01:35
  • Extension methods also do not extend methods, they extend classes (that is they pseudo-add additional methods to a class). – Erik Philips Sep 11 '15 at 02:51
  • @Jonesopolis I just want to add a parameter to "Index() "method, "Index() " method according to show the "viewName" of the return View. But can't modify the original method – Tomcat Sep 11 '15 at 03:59

2 Answers2

4

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.

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
0

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();
    }
}
  • 1
    Thank you. I want to do the following: 1、add new a view1.cshtml (A new theme) 2、Don't modify the original code old using: `@Html.Action("Index","HomeController");` updated: `@HtmlAction("Index","HomeController",{viewName="view1"})` – Tomcat Sep 11 '15 at 06:17
  • 3
    You have many ways to do this. For example: You could create your index and write to it @Html.Partial(partialViewName, this.Model) where partialViewName are your themes and it can be passed by ViewBag.partialViewName. In addition to this form there are several other which depends on the situation, for example, you could use jquery like this:.. $ ("#MyDiv").empty().load('@Url.Action("ActionName", "Controller" new {parameter = value}) '+' & nocache = '+ NewGuid); I'm out of time now, but I would write a reply with some good examples I have here. –  Sep 11 '15 at 14:22