Web Api Core - 无法从客户端发布

时间:2017-05-11 08:25:49

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

我是web api的新手,在使用POST创建新数据库条目时,我遇到了web api核心问题。 ajax调用看起来像;

chisq.test(y$Sum2,y$Sum3)

我的API方法看起来像;

addSelectedContract = function (contractId, url, callback) {
        // hard code url to get working
        url = "http://localhost:17972/api/selectedcontracts";
        var contract = JSON.stringify({ contractId: contractId });
        $.ajax({
            type: "Post",
            url: url,
            data: contract,
            contentType: "application/json"
        }).done(callback(status));
        };

来自powershell的测试给出了这个结果;

namespace Properties.API.Controllers
{
    [Route("api/selectedcontracts")]
    public class SelectedContractController : BaseController
    {
        private readonly ISirUoW SirUoW;
        private readonly SirDb Db;

        public SelectedContractController(SirDb db, ISirUoW repo, Services.IMailService mailService)
            : base(mailService)
        {
            this.Db = db;
            this.SirUoW = repo;
        }

        [HttpPost()]
        public IActionResult Post([FromBody] SelectedContract contract)
        {
            try
            {
                this.SirUoW.SelectContract.Add(contract);
                return Ok(new OperationStatus
                {
                    Status = true,
                    OperationID = contract.SelectedContractId
                });
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                var status = Mapper.Map<OperationStatus>(e);
                return Ok(status);
            }
        } 

SelectedContract类定义为; 命名空间SIR.Domain.Poco {     使用接口;     使用系统;     使用System.ComponentModel.DataAnnotations;

Invoke-RestMethod http://localhost:xxxx/api/selectedcontracts -Method POST -Body (@{contractId="7"} | ConvertTo-Json) -ContentType "application/json"
[ERROR] Invoke-RestMethod : The remote server returned an error: (500) Internal Server Error.
[ERROR] At line:1 char:1
[ERROR] + Invoke-RestMethod http://localhost:xxxx/api/selectedcontracts -Method POST -Bod ...
[ERROR] + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[ERROR]     + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke- 
[ERROR]    RestMethod], WebException
[ERROR]     + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRes 
[ERROR]    tMethodCommand
[ERROR]  

} 我搜索过代码示例,但我看不出我做错了什么。

编辑:虽然我得到了相同的结果,但我做了一些改变。 SelectedContract类定义为;

public class SelectedContract : IAuditCreated
{
    [Key]
    public int SelectedContractId { get; set; }

    public int ContractId { get; set; }

    public DateTime Created { get; set; }

    [MaxLength(50)]
    public string CreatedBy { get; set; }
}

但是我只发送一个字段,ContractId。我不想发送主键或审计字段。     所以我创建了一个DTO类;

    namespace SIR.Domain.Poco
    {
        using Interfaces;
        using System;
        using System.ComponentModel.DataAnnotations;

        public class SelectedContract : IAuditCreated
        {
            [Key]
            public int SelectedContractId { get; set; }

            public int ContractId { get; set; }

            public DateTime Created { get; set; }

            [MaxLength(50)]
            public string CreatedBy { get; set; }
        }
    }

并在API中更改了Post方法的签名;

    namespace Properties.API.Models
    {
        public class SelectedContractDto
        {
            public int ContractId { get; set; }
        }
    }

仍然收到500内部服务器错误。 我看了一下Fiddler并发现了这个错误

        [HttpPost()]
        public IActionResult Post([FromBody] SelectedContractDto contract)
        {
            var selectedContract = new SelectedContract { ContractId = Convert.ToInt32(contract.ContractId) };
            try
            {
                this.SirUoW.SelectContract.Add(selectedContract);
                return Ok(new OperationStatus
                {
                    Status = true,
                    OperationID = selectedContract.SelectedContractId
                });
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                var status = Mapper.Map<OperationStatus>(e);
                return Ok(status);
            }
        }

我无法在线找到有关此错误的任何内容。

1 个答案:

答案 0 :(得分:0)

我假设您调用的是方法,但之后有错误,而try-catch块中没有显示?检查&#34;公共语言运行时异常&#34;在调试 - &gt; Windows - &gt;例外设置,然后重试。

发布结果。