【问题标题】:Deployed Aspnet WebApi to IIS OK but return 500 internal Server Error When CRUD将 Aspnet WebApi 部署到 IIS 正常,但在 CRUD 时返回 500 内部服务器错误
【发布时间】:2018-04-15 12:43:58
【问题描述】:

我刚刚开始使用 asp.net Core 2.0 web api。请提供您的帮助。

我已成功将 asp.net WebApi 应用程序部署到本地 IIS 和 我用下面的代码在 Sql Server 2017(免费版)中创建了一个数据库。

问题:当我使用 PostMan 发帖时出现状态 500 内部服务器错误

  1) in StartUp.cs with ConnectionString:

     public void ConfigureServices(IServiceCollection services)
     {
      services.AddMvc();

      services.AddDbContext<ContactContext>(option => option.UseSqlServer(Configuration.GetConnectionString("Default")));
     }


    2) connection string in appsettings.json

    "connectionstrings": {
     "Default": "Server = .\\SQL2017Express; Database = ContactDB; Integrated Security = True;"
      }


    **The Problems Encountered:** 

    problem: status 500 internal server error when I do a post with PostMan 

    Post : http://192.168.1.8:8989/api/contacts

    Json Data for the Body in PostMan:
    {
    "FirstName" : "John",
    "LastName" : "David",
    "MobilePhone" : "123456789"
    }

// controller
    [Produces("application/json")]
        [Route("api/[controller]")]
        public class ContactsController : Controller
        {
            public IContactRepository ContactsRepo { get; set; }

            public ContactsController(IContactRepository _repo)
            {
                ContactsRepo = _repo;
            }

            [HttpGet]
            public async Task<IActionResult> GetAll()
            {
                var contactList = await ContactsRepo.GetAll();
                return Ok(contactList);
            }

            [HttpGet("{id}", Name = "GetContacts")]
            public async Task<IActionResult> GetById(string id)
            {
                var item = await ContactsRepo.Find(id);
                if (item == null)
                {
                    return NotFound();
                }
                return Ok(item);
            }

            [HttpPost]
            public async Task<IActionResult> Create([FromBody] Contacts item)
            {
                if (item == null)
                {
                    return BadRequest();
                }
                await ContactsRepo.Add(item);
                return CreatedAtRoute("GetContacts", new { Controller = "Contacts", id = item.MobilePhone }, item);
            }

            [HttpPut("{id}")]
            public async Task<IActionResult> Update(string id, [FromBody] Contacts item)
            {
                if (item == null)
                {
                    return BadRequest();
                }
                var contactObj = await ContactsRepo.Find(id);
                if (contactObj == null)
                {
                    return NotFound();
                }
                await ContactsRepo.Update(item);
                return NoContent();
            }

            [HttpDelete("{id}")]
            public async Task<IActionResult> Delete(string id)
            {
                await ContactsRepo.Remove(id);
                return NoContent();
            }
        }

这两种连接字符串我都试过了:

a)
"connectionstrings": {
"Default": "Server = .\\SQL2017Express; Database = ContactDB; Integrated Security = True;"
},
b)
"connectionstrings": {
"Default": "Server = .\\SQL2017Express; Database = ContactDB; User Id=sa; Password=xxxxx ; Integrated Security = True;"
},

【问题讨论】:

    标签: asp.net-core-2.0 asp.net-core-webapi


    【解决方案1】:

    在我看来,您在连接到 SQL 实例时遇到了问题。除非您运行多个 SQL Server 实例,否则最好卸载该软件并将其安装为默认实例。这只是使配置变得更加容易。

    如果您的网络服务器上安装了 SSMS(Sql Server Management Studio),请打开并输入 select @@servername,这会给出您实际实例的名称。

    你也可以在服务中看到名字,我有一个默认实例,所以你看不到名字

    在将其部署到生产环境之前,请确保您了解 SQL Server Express 的性能和大小限制。

    你有没有看过 SQL Server 配置管理器(你可能需要安装它)

    您需要确保如果您不使用 TCP 而是使用 DMA 或 Pipes 来使用正确的连接字符串;你考虑过吗:

    \\<computer_name>\pipe\MSSQL$<instance_name>
    

    看看this URL

    【讨论】:

    • @Walter:听从你的建议,我检查了我的设置是:共享内存:启用,命名管道:禁用,TCP/IP:禁用。我只安装了一个 SQL2017Express。但我仍然不能用 WebApi 做 CRUD。连接字符串是正确的。
    • 您可以将错误记录到日志文件吗?您所做的是在 ContactsController 构造函数中添加参数 ILogger 记录器并将其保存为类字段。然后将执行包装在 try catch 中并像这样 catch (Exception e) { _logger.LogError( e) } 记录错误,更新 webconfig 后您将在磁盘上看到错误
    • 你需要去网络服务器看看你发布应用程序的位置会有一个 web.config 文件导航到 webconfig 中的 元素并设置 stdoutLogEnabled="true" stdoutLogFile="D :\Inetpub\log\WebAppName" 其中 webname 是您喜欢的任何名称 该目录应该存在并且不应该向公众提供,因为导致错误的黑客会读取它以获取有关可能的攻击向量的信息
    • @Walter:我在 C 盘的部署文件夹中有 web.config 文件。 WebServer 中的网站在 AsNetCoreModule 中也有一个 web.config。我可以更改 C 盘和发布中的那个吗?我应该改变哪一个?只需设置 stdoutLogEnabled = true,而 stdoutLogFile=".\logs\stdout" 保持不变。
    • @Walter 我可以在实现 ILogger 后获取日志文件,而无需更改 stdoutLogEnabled = true。我没有更改 web.config 中的任何内容。我的错误是 502.3 Bad Gateway。如何解决这个问题?这个错误被用于测试 Api 的 PostMan 捕获。
    猜你喜欢
    • 1970-01-01
    • 2011-03-07
    • 2016-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-12
    • 2013-07-24
    相关资源
    最近更新 更多