简体   繁体   English

在MVC 2中将ELMAH与区域一起使用

[英]Using ELMAH with Areas in MVC 2

I'm following this blog post about setting up ELMAH with MVC: 我正在关注有关使用MVC设置ELMAH的博客文章:

http://dotnetdarren.wordpress.com/2010/07/27/logging-on-mvc-part-1/ http://dotnetdarren.wordpress.com/2010/07/27/logging-on-mvc-part-1/

I've only done part 1. Everything works correctly if I simply go to the Home controller, and then cause an error. 我只完成了第1部分。如果我只是转到Home控制器,然后导致错误,那么一切都将正常工作。 I can view /elmah.axd as well, and my errors are logging in the database correctly. 我也可以查看/elmah.axd,我的错误正在正确地记录在数据库中。

However, I've added an area to my application named Admin. 但是,我在应用程序中添加了一个名为Admin的区域。 If I navigate to /Admin, I receive the following error: 如果导航到/ Admin,则会收到以下错误:

System.MissingMethodException: No parameterless constructor defined for this object. System.MissingMethodException:没有为此对象定义无参数的构造函数。

public override IController CreateController(RequestContext requestContext, string controllerName)
{
    var controller = base.CreateController(requestContext, controllerName); //Error here

    var c = controller as Controller;

I'm assuming this has something to do with my Area. 我假设这与我的地区有关。

Does anyone know how to fix this? 有谁知道如何解决这一问题?

EDIT for jfar: 编辑jfar:

For instance, I have an Employees controller in my Admin area: 例如,我在“管理”区域中有一个“雇员”控制器:

public class EmployeesController : Controller
{
    private IEmployeesRepository employeesRepository;

    public EmployeesController(IEmployeesRepository employeesRepository)
    {
        this.employeesRepository = employeesRepository;
    }

    //...
}

Does your AdminController have a parameterless constructor? 您的AdminController是否具有无参数的构造函数?


Yes, you'll need to have a parameterless constructor. 是的,您将需要一个无参数的构造函数。 If you are trying to inject dependencies you'll need to write or use a controller factory which knows how to do this. 如果您尝试注入依赖项,则需要编写或使用知道如何执行此操作的控制器工厂。

This may help: 这可能会有所帮助:

Constructor Dependency Injection in a ASP.NET MVC Controller ASP.NET MVC控制器中的构造方法依赖项注入

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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