POST 使用 .Net Core 3.1 的请求和 Power Automate 的 HTTP 请求

POST Request using .Net Core 3.1 and HTTP Request of Power Automate

我在使用 .Net Core 3.1 MVC 从 Power Automate 发送 post 请求时遇到问题。我创建了一个具有参数名称和年龄的简单人员应用程序,然后视图有一个按钮,当您单击该按钮时,它会发送对 Power Automate HTTP 请求提供的 POST 请求的回复,我将收到一封包含名称的电子邮件和我为人物模型设置的年龄。但每次我收到一封电子邮件时,它的姓名和年龄都是空的。下面提供一些详细信息是我的源代码:

Index.cshtml

<a asp-controller="Test" asp-action="SendNameAge" class="btn btn-success" >Json Trigger</a>

Success.cshtml

<p>success request</p>

Failed.cshtml

<p>Failed request</p>

Model.cs

using System;
using System.Collections.Generic;
using System.Text;

namespace WebApplication.Model
{
    public class Person
    {
        public string Name { get; set; }
        public int Age { get; set; }
    }
}

TestController.cs

using Microsoft.AspNetCore.Mvc;
using System;
using System.Net.Http;
using System.Threading.Tasks;
using WebApplication.Model;
using WebApplication.Utility;

namespace WebApplication.Controllers
{
    public class TestController : Controller
    {
        public IActionResult Index()
        {
            return View();
        }

        public IActionResult Success()
        {
            return View();
        }

        public IActionResult Failed()
        {
            return View();
        }

        public async Task<IActionResult> SendNameAge()
        {
            using (var client = new HttpClient())
            {
                Person person = new Person
                {
                    Name = "Juan Dela Cruz",
                    Age = 32
                };

                client.BaseAddress = new Uri(SD.ApiUri);
                var response = await client.PostAsJsonAsync(SD.ApiUri, person);

                if (response.IsSuccessStatusCode)
                {
                    return RedirectToAction(nameof(Success), Json(response));
                }
                else
                {
                    return RedirectToAction(nameof(Failed), Json(response));
                }
            }
        }
    }
}

然后这是我点击按钮时收到的电子邮件,也是我应该收到的带有参数的电子邮件,但我只是使用 Postman 进行测试。

来自 .Net Core 的电子邮件

使用 Postman 发送电子邮件

尝试将您的 Person 模型转换为 JSON 并仅使用简单的 PostAsync

Person person = new Person
{
    Name = "Juan Dela Cruz",
    Age = 32
};
var personJSON = JsonConvert.SerializeObject(person);
var buffer = System.Text.Encoding.UTF8.GetBytes(personJSON);
var byteContent = new ByteArrayContent(buffer);
byteContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");

var response = await client.PostAsync(SD.ApiUri, byteContent);