簡體   English   中英

HttpClient PostAsync() 不起作用,而 POSTMAN 工作正常

[英]HttpClient PostAsync() doesn't work while POSTMAN works fine

我正在嘗試在我的 Azure 數據庫帳戶上發布項目。 我寫了一個ASP.Net Core web應用程序並發布到Azure帳戶,它實現了Get、Post、Put和Delete等方法。 在我的 Xamarin Forms 項目中,我正在嘗試調用 HttpClient PostAsync 方法在 Azure 數據庫中創建(發布)一條記錄。 在 POSTMAN 上測試相同的事務可以正常工作,但在我的 Xamarin Forms 移動應用程序中不起作用。 GetStringAsync 方法在 POSTMAN 和我的移動應用程序上都可以正常工作。 我不知道為什么 PostAync 不起作用。 請幫忙!

這是調用 PostAsync 的事件處理程序方法:

async void OnAdd(object sender, EventArgs e)
{
    // This item is to be posted to the Azure database.
    var tblmoney_item = new TblMoney
    {
        RecordDate = Convert.ToDateTime("7-26-2020"),
        ItemDesc = "coffee",
        AmountOut = 4500,    // The currency unit is in KRW. (KRW 4,500 = US$4).
        SpentTypeId = 3
    };

    // Serialize the item in JSON format.
    var content = JsonConvert.SerializeObject(tblmoney_item);

    // POST the item to the URL. Works fine on POSTMAN, but does NOT work on my mobile app.
    await _client.PostAsync(Url, new StringContent(content));
    
    // Refresh the list view.
    _tblmoney_items.Insert(0, tblmoney_item);
}

這是上面變量(屬性)的 MainPage class 中的聲明:

private const string Url = "https://testwebapi.azurewebsites.net/tblmoney";
private HttpClient _client = new HttpClient();
private ObservableCollection<TblMoney> _tblmoney_items;

該項目的 Class 聲明如下:

public partial class TblMoney
{
    public int Id { get; set; }
    public DateTime? RecordDate { get; set; }
    public string ItemDesc { get; set; }
    public int? AmountOut { get; set; }
    public int? SpentTypeId { get; set; }

    public virtual TblSpentType SpentType { get; set; }
}

Here's the Controller Class in the REST Web API project, which is an ASP.Net Core Web Application project.

[ApiController]
[Route("[controller]")]
[Produces("application/json")]

public class TblMoneyController : ControllerBase
{
    private mynotedbContext _context = new mynotedbContext();

    [HttpGet]
    public IEnumerable<TblMoney> Get()
    {
        // get all tblMoney items.
        return _context.TblMoney.ToList();
    }

    [HttpPost]
    public IActionResult Post([FromBody]TblMoney tblmoney_item)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }

        _context.TblMoney.Add(tblmoney_item);
        _context.SaveChanges(true);
        return StatusCode(StatusCodes.Status201Created);
    }
}

數據庫上下文如下所示:

public partial class mynotedbContext : DbContext
{
    public mynotedbContext()
    {
    }

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

    public virtual DbSet<TblMoney> TblMoney { get; set; }
...

(來自評論)

我建議您將Content-Type header 設置為application/json - 您可以通過在客戶端上設置 header 或使用聲明類型的 stringcontent 重載來執行此操作:例如new StringContent(content, Encoding.UTF8, "application/json"))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM