3
Good afternoon, I am using ASP.NET MVC 5 with Windsor and when an Ajax request calls a method in the controller, the error informing that the controller has unsatisfied dependencies. How do I work the request since the dependencies are solved in the constructor?
In Controller I declare variables:
private readonly IMaterial _material;
private readonly IFamilia _familia;
private readonly IUnidade _unidade;
private readonly IAlterarMaterial _alterarMaterial;
Then I have the Controller’s Maker:
public MaterialController(IMaterial material, IFamilia familia, IUnidade unidade, IAlterarMaterial alterarMaterial)
{
_material = material;
_unidade = unidade;
_familia = familia;
_alterarMaterial = alterarMaterial;
}
And then I have the method that will be called by Ajax.
public ActionResult ListarMaterial(String nome)
{
List<Material> lista = _material.Listar(nome).ToList();
return View(lista);
}
At Global.asax I have these two methods
private static void RegisterWindsor()
{
container = new WindsorContainer().Install(FromAssembly.This());
var controllerFactory = new WindsorControllerFactory(container.Kernel);
ControllerBuilder.Current.SetControllerFactory(controllerFactory);
}
protected void Application_End()
{
container.Dispose();
}
And in the applications_Start
RegisterWindsor();
Class Controllerfactory
public class WindsorControllerFactory : DefaultControllerFactory
{
private readonly IKernel kernel;
public WindsorControllerFactory(IKernel kernel)
{
this.kernel = kernel;
}
public override void ReleaseController(IController controller)
{
kernel.ReleaseComponent(controller);
}
protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
{
if (controllerType == null)
{
throw new HttpException(404, string.Format("The controller for path '{0}' could not be found.", requestContext.HttpContext.Request.Path));
}
return (IController)kernel.Resolve(controllerType);
}
}
Called Ajax
function Pesquisar() {
$('.corpoTbl').remove();
$.ajax({
url: "/Material/ListarMaterial",
type: "POST",
data: { nome: $('#pesquisa').val() },
success: function (data) {
if (!data.ok){
window.alert("erro");
}
else {
$('#tabela').html(data);
}
}
});
You could post a piece of your code to understand how this dependency injection is done?
– Leonel Sanches da Silva
Missing command using for the Windsor act.
– Leonel Sanches da Silva
And what command would that be?
– Alan Almeida
I don’t know. You had to set up Windsor. That’s what I want to know.
– Leonel Sanches da Silva
I edited the question and put the Windsor configuration files, when I use the system normally and the requests are made in the traditional way, IE, the form page, works normally, only when I make an Ajax request that does not work.
– Alan Almeida
Apparently it’s all right. How is the call to Ajax?
– Leonel Sanches da Silva