在创建新控制器以引用其他控制器之前,我的 Swagger 显示错误

问题描述 投票:0回答:1

我创建了一个新控制器,以便我可以在其中放入一个函数并在所有控制器中使用它,并且所有控制器都继承自该控制器,但我收到错误。

我的 Swagger UI 显示此错误:

获取错误 内部服务器错误 https://localhost:7032/swagger/v1/swagger.json

压套原木有: Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[1] 执行请求时发生未处理的异常。

BaseController

using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace TodoList.Api.Controllers
{
    public class BaseController : ControllerBase
    {
       public string GetId()
        {
            if (HttpContext.User.Identity is ClaimsIdentity identity)
            {
                if (identity != null)
                {
                    var IdUser = identity?.FindFirst(ClaimTypes.SerialNumber).Value;
                    return IdUser!;
                }
            }

            return null!;
        }
    }
}

其他控制器:

using System;
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
using System.Linq;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;
using AutoMapper;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.IdentityModel.Tokens;
using TodoList.Api.DbContextFolder;
using TodoList.Api.Entity;
using TodoList.Api.Model;

namespace TodoList.Api.Controllers
{
    [ApiController]
    [Route("api/TaskManage")]
    [Authorize]
    public class TaskController : BaseController
    {
        private readonly TaskDbContext taskDbContext;
        private readonly IMapper _mapper;
                    
        public TaskController(TaskDbContext taskDbContext,IMapper mapper)
        {
            this.taskDbContext = taskDbContext;
            this._mapper = mapper;
        }

        // GET: api/values
        [HttpGet("All")]
        public async Task<ActionResult<ICollection< TaskEntity>>> GetAllTask()
        { 
            var result = await taskDbContext.TaskDb.ToListAsync();

            if (HttpContext.User.Identity is ClaimsIdentity identity)
            {
                var IdUser = identity?.FindFirst(ClaimTypes.SerialNumber).Value;
            }

            return Ok(result);
        }

        // GET api/id
        [HttpGet("{id}")]
        public async Task<ActionResult<TaskEntity>> GetBuyId(int id)
        {
            var result = await taskDbContext.TaskDb.FirstOrDefaultAsync(c => c.Id == id);

            if (result == null)
            {
                return NotFound(result);
            }

            return Ok(result);
        }

        // POST api/Create
        [HttpPost("Create")]
        public async Task<ActionResult<TaskEntity>> CreateToDo(TaskSendDTO T)
        {
            if (T != null)
            {
                var IdUser = GetId();
                //var IdUser = User.Claims.First(x => x.Type == "Id").Value;
                
                var DataMapper = _mapper.Map<TaskEntity>(T);

                DataMapper.createById = Convert.ToInt32(IdUser);

                //DataMapper.createBy = taskDbContext.UsersAccounts.FirstOrDefault(x => x.UserId == Convert.ToInt32(IdUser));

                await taskDbContext.TaskDb.AddAsync(DataMapper);

                taskDbContext.SaveChanges();

                return Ok(DataMapper);
            }

            return BadRequest("Something went wrong");
        }

        // PUT api/NameUpdate/id
        [HttpPut("NameUpdate/{id}")]
        public async Task<ActionResult<TaskUpdateTitle>> UpdateNameToDo(int id,string value)
        {
            var FindId = taskDbContext.TaskDb.Find(id);

            if (FindId == null)
            {
                return NotFound(FindId);
            }

            var DataNew = await taskDbContext.TaskDb.Select(e =>  e.Title).FirstOrDefaultAsync();
            FindId.Title = value;

            if (FindId.Title is not null)
            {
                //taskDbContext.Update(FindId);
                taskDbContext.SaveChanges();
                return Ok(FindId.Title);
            }

            return NotFound();
        }

        //put api/IsDoneUpdate/Id
        [HttpPut("IsDoneUpdate")]
        public async Task<ActionResult<TaskUpdateIsDoneDTO>> UpdateIsDone(int[] ids)
        {
            var tasks  = await
                taskDbContext
                .TaskDb
                .Where(t=> ids.Contains(t.Id)).ToListAsync();

            if (ids.Length != tasks.Count)
            {
                return NotFound(tasks);
            }
            
            foreach (var task in tasks)
            {
                task.IsDone = true;
            }

            taskDbContext.SaveChanges();

            return Ok();
        }

        //DELETE api/TaskId
        [HttpDelete("{TaskId}")]
        public async Task<ActionResult<TaskEntity>> DeleteBuyId(int TaskId)
        {
            var result = await taskDbContext.TaskDb.FindAsync(TaskId);

            if (result == null)
            {
                return NotFound(result);
            }

            taskDbContext.Remove(result);
            taskDbContext.SaveChanges();

            return NoContent();
        }
    }
}
c# asp.net-core .net-core asp.net-core-webapi asp.net-core-7.0
1个回答
0
投票

为了避免在 API 列表中显示 GetId 方法并防止与控制器中同名的其他方法发生冲突,您只需在基础中的 GetId 方法上方添加 [ApiExplorerSettings(IgnoreApi = true)] 属性控制器:

[ApiExplorerSettings(IgnoreApi = true)]
public string GetId()
{
....
}
© www.soinside.com 2019 - 2024. All rights reserved.