Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在asp.net mvc中添加BaseController时出错_C#_Asp.net Mvc_Asp.net Web Api - Fatal编程技术网

C# 在asp.net mvc中添加BaseController时出错

C# 在asp.net mvc中添加BaseController时出错,c#,asp.net-mvc,asp.net-web-api,C#,Asp.net Mvc,Asp.net Web Api,我有一个这样的模块控制器。(ASP Web API控制器)。(工作正常) 然后将我的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.Contro

我有一个这样的模块控制器。(ASP Web API控制器)。(工作正常)

然后将我的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);
        } 
    }
}
使用System.Collections.Generic;
使用System.Web.Http;
使用AMMS.Logger.interface;
使用AMMS.Service.interface;
使用AMMS.Service.Models;
名称空间AMMS.Api.Controllers
{
公共类ModuleController:BaseController
{
专用只读IModuleService(U moduleService);
专用只读ILoggerService(日志服务);
公共模块控制器(IModuleService模块服务,ILoggerService loggerService):基本(loggerService)
{
_模块服务=模块服务;
_loggerService=loggerService;
}
公共列表获取(字符串用户ID)
{
return _moduleService.GetModules(userId);
} 
}
}
但是在添加BaseController之后,它无法加载模块。 当我调试时,ModuleControl的代码似乎没有执行! 在客户端,我得到--

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


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

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

BaseController 更改
专用只读ILoggerService\u loggerService
受保护的只读ILoggerService\u loggerService(如果要在派生类中访问它)

模块控制器:

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);
        } 
    }
}
使用System.Collections.Generic;
使用System.Web.Http;
使用AMMS.Logger.interface;
使用AMMS.Service.interface;
使用AMMS.Service.Models;
名称空间AMMS.Api.Controllers
{
公共类ModuleController:BaseController
{
专用只读IModuleService(U moduleService);
公共模块控制器(IModuleService模块服务,ILoggerService loggerService):基本(loggerService)
{
_模块服务=模块服务;
}
公共列表获取(字符串用户ID)
{
return _moduleService.GetModules(userId);
} 
}
}

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

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


很高兴帮助你

可能是因为您已在此BaseController和ModuleController中声明了_loggerService属性?尝试将BaseController中的\u loggerService的可见性更改为internal,并从ModuleControllery中删除\u loggerService属性您不应该在派生类中接触\u loggerService,请调用基类构造函数。谢谢,它成功了@JohnmChange to internal没有帮助:(@bolt19请参阅下面我的答案
protected
可能比
internal
更好,如果您希望派生类可以访问它。好的,我假设您使用的是visualstudio,请尝试“生成>清除解决方案”,生成>生成解决方案,然后检查错误列表。就是这样:)。我忘了将它添加到注册表类型
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);
        } 
    }
}
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);
        } 
    }
}
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);
        } 
    }
}
protected BaseController()
{
    //
} 
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)  
    {  

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

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