Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用.NETCore3.1和PowerAutomation的HTTP请求发布请求_C#_Json.net_Httprequest_Asp.net Core 3.1_Power Automate - Fatal编程技术网

C# 使用.NETCore3.1和PowerAutomation的HTTP请求发布请求

C# 使用.NETCore3.1和PowerAutomation的HTTP请求发布请求,c#,json.net,httprequest,asp.net-core-3.1,power-automate,C#,Json.net,Httprequest,Asp.net Core 3.1,Power Automate,我遇到一个问题,使用.Net Core 3.1 MVC在Power Automation的post请求中发送参数。我创建了一个简单的person应用程序,该应用程序具有参数Name和Age,然后视图中有一个按钮,当您单击它时,该按钮将发送对Power Automation HTTP request提供的POST请求的回复,我将收到一封带有我设置为person模型的姓名和年龄的电子邮件。但每次我收到一封电子邮件,它都有一个空的名字和年龄。下面提供一些详细信息是我的源代码: Index.cshtml

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

Index.cshtml

<a asp-controller="Test" asp-action="SendNameAge" class="btn btn-success" >Json Trigger</a>
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));
                }
            }
        }
    }
}
使用Microsoft.AspNetCore.Mvc;
使用制度;
使用System.Net.Http;
使用System.Threading.Tasks;
使用WebApplication.Model;
使用WebApplication.Utility;
命名空间WebApplication.Controllers
{
公共类TestController:控制器
{
公共IActionResult索引()
{
返回视图();
}
公共IActionResult成功()
{
返回视图();
}
公共IActionResult失败()
{
返回视图();
}
公共异步任务发送名称()
{
使用(var client=new HttpClient())
{
人=新人
{
Name=“胡安·德拉·克鲁兹”,
年龄=32
};
client.BaseAddress=新Uri(SD.ApiUri);
var response=wait client.postsjsonasync(SD.ApiUri,person);
if(响应。IsSuccessStatusCode)
{
返回RedirectToAction(nameof(Success)、Json(response));
}
其他的
{
返回RedirectToAction(nameof(失败),Json(响应));
}
}
}
}
}
这是我点击按钮时收到的电子邮件,也是我应该收到的带有参数的电子邮件,但我只是使用邮递员进行测试

来自.Net Core的电子邮件

使用邮递员发送电子邮件

尝试将您的个人模型转换为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);
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; }
    }
}

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));
                }
            }
        }
    }
}
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);