【问题标题】:Error developing web project + api + SQL Server + dotnet开发 Web 项目 + api + SQL Server + dotnet 时出错
【发布时间】:2020-04-07 01:28:53
【问题描述】:

我必须开发一个访问 SQL Server 表并在 HTTP-REST 上返回 JSON 的小项目。我测试了一个直接项目来访问我的program.cs 直接数据库并且它可以工作,但是当我尝试集成一个基本的 Web API 项目时,我的项目在索引或GET 路由中都没有出现。

基本配置(appsettings.json):

"ConnectionStrings": {
    "ConnectAppDatabase": "Server=<my_domain>.database.windows.net;Database=<my_base>;user id=<my_user>;password=<my_password>"
  }

罗塔 (launchSettings.json)

..."connect_app": {
      "commandName": "Project",
      "launchBrowser": true,
      "launchUrl": "connectAppController",
      "applicationUrl": "https://localhost:5001;http://localhost:5000",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }

(启动.cs)

public void ConfigureServices(IServiceCollection services)
{
    services.AddDbContext<connectAppContext>(options => options.UseSqlServer(Configuration.GetConnectionString("ConnectAppDatabase")));
    services.AddControllers();
}

在我的控制器中,索引只是一个静态页面,在获取路由中我想返回查询

https://localhost:5001/api/connectapp/getmensage?codigo=<code_example>

namespace Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class connectAppController : Controller
    {
        private readonly connectAppContext _context;

        public connectAppController(connectAppContext context)
        {
            _context = context;
        }

        public IActionResult Index()
        {
            Console.Write("Index");
            Console.ReadLine();
            return View();
        }

        [HttpGet("{codigo:length(12)}", Name = "getmessage")]
        public ActionResult<byte[]> Get(string cod)
        {
            if (cod == null)
            {
                return NotFound();
            }

            var mensage = _context.mensages.FirstOrDefault(m => m.codigo == cod);
            if (mensage == null)
            {
                return NotFound();
            }

            byte[] jsonUtf8Bytes;

            var options = new JsonSerializerOptions
            {
                WriteIndented = true
            };

            jsonUtf8Bytes = JsonSerializer.SerializeToUtf8Bytes(mensage, options);
            return jsonUtf8Bytes;
        }
    }
}

当我运行项目时,它不会返回任何错误,但是在访问 url 时,我只收到默认消息 404 - 找不到页面

shinier01@shinier01:~/Projetos/Connect-cargo/connect-app$ dotnet run info: Microsoft.Hosting.Lifetime[0]
      Now listening on: https://localhost:5001 info: Microsoft.Hosting.Lifetime[0]
      Now listening on: http://localhost:5000 info: Microsoft.Hosting.Lifetime[0]
      Application started. Press Ctrl+C to shut down. info: Microsoft.Hosting.Lifetime[0]
      Hosting environment: Development info: Microsoft.Hosting.Lifetime[0]
      Content root path: /home/shinier01/Projetos/Connect-cargo/connect-app

有谁知道我做错了什么?如果手是这样,我如何连接 SQL Server 数据库?或者我的函数中的一些配置/命令?

更新

从我的调试中可以看出,它在路由https://localhost:5001 上打开网络浏览器,出现错误消息,但从未通过Index() 中的断点

【问题讨论】:

  • 您有多个返回 NotFound 的代码。你试过debugging your app吗?设置断点以查看代码的行为方式?还是添加日志记录?
  • 但是当索引访问没有什么可以返回时没有找到
  • 当我调试并尝试调用我的路由时,我的控制台上什么也没有出现
  • 我在调试控制台上注意到的唯一一件事是,时不时会弹出此消息“”线程 7676 以代码 0 终止“
  • 什么“错误信息”?

标签: asp.net .net sql-server .net-core


【解决方案1】:

改变

[Route("api/[controller]")]

[Route("api")]

然后运行应用程序并调用

https://localhost:5001/api/getmessage?codigo=<cod_exemple>

【讨论】:

    【解决方案2】:

    我至少可以让我的页面访问路由并显示调试,但现在返回错误

    invalidOperationException: 没有服务类型 'Microsoft.AspNetCore.Mvc.ViewFeatures.ITempDataDictionaryFactory' 有 已注册。

    我评论了我的控制器

    //[Route("api")]
    //[ApiController]
    

    并将其添加到我的 Startup.cs 配置中

    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllerRoute(
              name: "default",
              pattern: "{controller=connectApp}/{action=Index}/");
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-21
      • 1970-01-01
      • 2011-07-13
      • 2018-10-15
      • 2013-07-09
      • 1970-01-01
      相关资源
      最近更新 更多