ASP.NET MVC Html.RenderAction method
I try to use the method something like this:
<%Html.RenderAction<NavigatorController>(n => n.Menu());%>
but got an exception "A public action method 'Menu' could not be found on controller 'WebUI.Controllers.ProductsController'. Why compiler try to find it in ProductsController, if I specify NavigatorController for this purposes? Code in my NavigatorController very simple:
namespace WebUI.Controllers
{
public class NavigatorController : Controller
{
public string Menu()
{
return "NavigatorController here";
}
}
}
P.S. I use RenderAction from ASP.NET MVC Features library.
I resolved this issue. As @jfar mentioned error was something else, error was in my custom controller factory module, that I wrote using Castle.Windsor library, I tried to implement inversion of control describing in Steven Sanderson book (Pro ASP.NET MVC Framework). So, my method that register all Controller types was:
_container = new WindsorContainer(new XmlInterpreter(new ConfigResource("castle")));
IEnumerable<Type> controllerTypes = from type in Assembly.GetExecutingAssembly().GetTypes()
where typeof(IController).IsAssignableFrom(type)
select type;
container.Register(Component.For(controllerTypes).LifeStyle.Is开发者_StackOverflow(LifestyleType.Transient));
After reading some article I replace it by this:
_container.Register(AllTypes.FromAssembly(Assembly.GetExecutingAssembly())
.BasedOn<Controller>()
.Configure(c => c.LifeStyle.Transient.Named(c.Implementation.Name.ToLower())));
and all work fine now
you can use the following statement in your view.
<% Html.RenderAction("Menu", "Navigator"); %>
This error must be happening someplace else.
<%Html.RenderAction<NavigatorController>(n => n.Menu());%>
This code would always use the NavigationController.
Like jfar said:
This error must be happening someplace else.
Check your routes.
My guess is you modified your routes, and it's always picking the one for the ProductsController.
精彩评论