c# - 在 asp.net mvc 中添加 BaseController 会出现错误

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

我有一个像这样的ModuleController。 (ASP Web API Controller )。 (工作正常)

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);
        } 
    }
}

我想添加一个所有 Controller 都通用的功能。所以我添加了一个像这样的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的代码似乎没有执行。 ! 在客户端我得到--

Failed to load resource: the server responded with a status of 500 (Internal Server Error)

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

最佳答案

基础 Controller private readonly ILoggerService _loggerService; 更改为 protected readonly ILoggerService _loggerService;(如果您想在派生类中访问它)

模块 Controller :

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);
        } 
    }
}

关于c# - 在 asp.net mvc 中添加 BaseController 会出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40738494/

相关文章:

c# - 如何将字符串传递给 C# 网站引用的 VB.NET 项目函数?

c# - 使用 C# 执行忽略 xsl :output 的 xslt 转换

c# - 如何让 Ninject 2 为 LINQ to SQL DataContext 使用无参数构造函数?

c# - 从自托管的 WEB API 控制台应用程序返回 jsonp

c# - .NET 和 mySQL 的用户安全

c# - 动态组合算法

asp.net-mvc - MVC和Url.Action

jquery - 使用 jQuery 和 LINQ-to-Entities 在 ASP.NET MVC 客户端 View 中对 jqGrid 进行排序

c# - 如何路由 API 并使用查询字符串?

c# - ASP.NET Web API 中不允许使用 HTTP PUT