【问题标题】:POST Request using .Net Core 3.1 and HTTP Request of Power Automate使用 .Net Core 3.1 的 POST 请求和 Power Automate 的 HTTP 请求
【发布时间】:2020-10-29 15:17:48
【问题描述】:

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

索引.cshtml

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

成功.cshtml

<p>success request</p>

失败的.cshtml

<p>Failed request</p>

模型.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));
                }
            }
        }
    }
}

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

来自 .Net Core 的电子邮件

使用 Postman 发送电子邮件

【问题讨论】:

    标签: c# json.net httprequest asp.net-core-3.1 power-automate


    【解决方案1】:

    尝试将您的 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);
    

    【讨论】:

      猜你喜欢
      • 2022-07-21
      • 1970-01-01
      • 2022-12-22
      • 1970-01-01
      • 2013-06-14
      • 2021-09-11
      • 2020-12-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多