在asp.net mvc中添加BaseController会产生错误

时间:2016-11-22 09:46:16

标签: c# asp.net-mvc asp.net-web-api

我有这样的ModuleController。 (ASP Web API控制器)。 (它工作正常)

using System.Collections.Generic;
using System.Web.Http;
using AMMS.Logger.Interfaces;
using AMMS.Service.Interfaces;
using AMMS.Service.Models;

namespace AMMS.Api.Controllers
{
    public class ModuleController: ApiController
    {
        private readonly IModuleService _moduleService;

        public ModuleController(IModuleService moduleService) 
        {
            _moduleService = moduleService;
        }

        public List<AmmsModule> Get(string userId)
        {
            return _moduleService.GetModules(userId);
        } 
    }
}

我想添加一个对所有Controllers来说都很常见的函数。所以我添加了一个像这样的BaseController:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Reflection;
using System.Web.Http;
using AMMS.Logger;
using AMMS.Logger.Interfaces;

namespace AMMS.Api.Controllers
{
    public class BaseController : ApiController
    {
        private readonly ILoggerService _loggerService;

        public BaseController(ILoggerService loggerService)
        {
            _loggerService = loggerService;
        }

        protected virtual bool OnError(string actionName, MethodInfo methodInfo, Exception exception)
        {
            _loggerService.LogInfo($"Exception : {exception}, MethodInfo : {methodInfo}, actionName: {actionName}");
            return false;
        }
    }
}

然后将我的ModuleController更改为继承此BaseController

using System.Collections.Generic;
using System.Web.Http;
using AMMS.Logger.Interfaces;
using AMMS.Service.Interfaces;
using AMMS.Service.Models;

namespace AMMS.Api.Controllers
{
    public class ModuleController: BaseController
    {
        private readonly IModuleService _moduleService;
        private readonly ILoggerService _loggerService;

        public ModuleController(IModuleService moduleService, ILoggerService loggerService) : base(loggerService)
        {
            _moduleService = moduleService;
            _loggerService = loggerService;
        }

        public List<AmmsModule> Get(string userId)
        {
            return _moduleService.GetModules(userId);
        } 
    }
}

但是在添加BaseController之后,它无法加载模块。 当我调试时,ModuleController的代码似乎没有执行。 ! 在客户端我得到 -

  

无法加载资源:服务器响应状态为500   (内部服务器错误)

这可能是什么原因(在添加BaseController之后,代码根本没有被执行)?

4 个答案:

答案 0 :(得分:1)

请在BaseController中使用_loggerService的相同实例,而不是重新声明它。

答案 1 :(得分:1)

<强> BaseController private readonly ILoggerService _loggerService;更改为protected readonly ILoggerService _loggerService;(如果您想在派生类中访问它)

<强> ModuleController:

using System.Collections.Generic;
using System.Web.Http;
using AMMS.Logger.Interfaces;
using AMMS.Service.Interfaces;
using AMMS.Service.Models;

namespace AMMS.Api.Controllers
{
    public class ModuleController: BaseController
    {
        private readonly IModuleService _moduleService;

        public ModuleController(IModuleService moduleService, ILoggerService loggerService) : base(loggerService)
        {
            _moduleService = moduleService;
        }

        public List<AmmsModule> Get(string userId)
        {
            return _moduleService.GetModules(userId);
        } 
    }
}

答案 2 :(得分:0)

using System.Collections.Generic;
using System.Web.Http;
using AMMS.Logger.Interfaces;
using AMMS.Service.Interfaces;
using AMMS.Service.Models;

namespace AMMS.Api.Controllers
{
    public class ModuleController: BaseController
    {
        private readonly IModuleService _moduleService;

        public ModuleController(IModuleService moduleService)
        {
            _moduleService = moduleService;
        }

        public List<AmmsModule> Get(string userId)
        {
            return _moduleService.GetModules(userId);
        } 
    }
}

在BaseController中改为:

protected BaseController()
{
    //
} 

答案 3 :(得分:0)

您是否在引导程序中注册了模块?

尝试添加名为“Bootstrapper.cs”的类

public class Bootstrapper  
{  
    public static IUnityContainer Initialize()  
    {  
        var container = BuildUnityContainer();  
        DependencyResolver.SetResolver(new UnityDependencyResolver(container));  
        return container;  
    }  

    private static IUnityContainer BuildUnityContainer()  
    {  
        var container = new UnityContainer();  

        // register all your components with the container here  
        //This is the important line to edit  
        container.RegisterType<IModuleService, YourModuleService>();
        container.RegisterType<ILoggerService, YourLoggerService>();

        RegisterTypes(container);  
        return container;  
    }  

    public static void RegisterTypes(IUnityContainer container)  
    {  

    }  
}

然后初始化你的Bootstrapper:

public class MvcApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        RouteConfig.RegisterRoutes(RouteTable.Routes);

        //Initialize bootstrapper class
        Bootstrapper.Initialize();
    }
}

很高兴为您服务!