将Aspnet WebApi部署到IIS,但在CRUD

时间:2018-04-15 12:43:58

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

我刚刚开始使用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;"
},

1 个答案:

答案 0 :(得分:0)

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

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

您还可以在服务中看到该名称,我有一个默认实例,因此您看不到名称

-s swich defining the instance

在部署此产品之前,请确保您了解SQL server express的性能和大小限制。

您是否看过SQL Server配置管理器(您可能需要安装它)

Protocols

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

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

查看this URL