asp.net core Web API 控制器停止响应并出现错误 500

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

我正在为搜索功能制作一个 PoC。我已经有一段时间没有做过 Web API 了,现在我从本教程和其他教程开始,更改了连接数据库等所需的内容。

这是周五有效的事情之一,而周一早上却不起作用。

我创建了一个 Web API 项目,其中已经搭建了一个 ValuesController。所以我创建了一个名为 searchController 的新控制器。我写了一个只返回一个字符串的方法,如下所示:

[Produces("application/json")]
[Route("api/Search")]
public class SearchController : Controller
{
    private readonly DbContext _context;

    public SearchController(DbContext context)
    {
        _context = context;
    }

    // GET api/Search/search criteria
    [HttpGet("{q}")]
    public string Search(string q)
    {
        return $"this will be your answer to: {q}";
    }
}

我不知道为什么它有效以及为什么现在不起作用。我已将其更改为看起来与我在下面发布的值控制器完全相同。

值控制器

using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;

namespace AbAeterno.Controllers
{
    [Route("api/[controller]")]
    public class ValuesController : Controller
    {
        // GET api/values
        [HttpGet]
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }

        // GET api/values/5
        [HttpGet("{id}")]
        public string Get(int id)
        {
            return "value";
        }

        // POST api/values
        [HttpPost]
        public void Post([FromBody]string value)
        {
        }

        // PUT api/values/5
        [HttpPut("{id}")]
        public void Put(int id, [FromBody]string value)
        {
        }

        // DELETE api/values/5
        [HttpDelete("{id}")]
        public void Delete(int id)
        {
        }
    }
}

和搜索控制器

    using AbAeterno.Models;
using Microsoft.AspNetCore.Mvc;

namespace AbAeterno.Controllers
{
    [Route("api/[controller]")]
    public class SearchController : Controller
    {
        private readonly DbContext _context;

        public SearchController(DbContext context)
        {
            _context = context;
        }

        // GET api/Search/search criteria
        [HttpGet]
        public string Get()
        {
            return "this method is alive";
        }
    }
}

当我运行解决方案时,默认控制器是值并且它正在工作,当我将 url 更改为

http://localhost:52457/api/search/

它说该页面无法工作,错误 500,在 chrome 中。

更新startup.cs

我发现了两种注册连接字符串的方法,我都写了,但是第二种使用DI,不能按预期工作,并且在调用数据库时,连接字符串为空。我就是这样的。

  public void ConfigureServices(IServiceCollection services)
        {
            //Configure DB connection strings
            DbContext.ConnectionString = Configuration.GetConnectionString("DatabaseEF");

            //services.AddDbContext<DbContext>(opt => opt.UseSqlServer(Configuration.GetConnectionString("DatabaseEF")));


            // Add framework services.
            services.AddMvc();
        }

解决方案

我删除了构造函数和私有变量,它再次工作,按照@Yuri S的建议

c# asp.net localhost asp.net-core-webapi
2个回答
1
投票

虽然问题中有解决方案部分,但该部分没有考虑依赖注入,应视为临时解决方案。

如果你想使用依赖注入,你的文件应该如下所示:

数据库上下文

public partial class MyDbContext : DbContext
{
    public virtual DbSet<Table> Table { get; set; }

    public MyDbContext(DbContextOptions<MyDbContext> options) : base(options)
    {}
}

启动.cs

    public void ConfigureServices(IServiceCollection services)
    {
        //Configure DB connection strings
        services.AddDbContext<MyDbContext>(opt => opt.UseSqlServer(Configuration.GetConnectionString("MyDbEF")));

        // Add framework services.
        services.AddMvc();
    }

通过这两项更改,您的连接字符串已在 DI 管理器组件中注册,因此您可以在控制器中创建一个构造函数,用于接收、注入使用连接字符串配置的上下文对象。像这样

控制器构造函数

    private readonly MyDbContext _context;

    public SearchController(MyDbContext context)
    {
        _context = context;
    }

0
投票
I removed the constructor and private variable and it worked again, as suggested by @Yuri S :- if we removed the constructor and private variable how code constructor dependency work , Kindly  help me with code , i have same issue in my code -  
© www.soinside.com 2019 - 2024. All rights reserved.