如何使用RESTSHARP状态代码错误44在我的Web API ASP.NET CORE 6.0文件中发送两个参数C#的参数

发布于 2025-01-29 06:06:33 字数 1613 浏览 3 评论 0原文

我的API中有接收文件,int和字符串参数的方法。另外,我还有一个桌面WPF应用程序(用C#编写),其中用户在选择一些选项后单击按钮,然后使用RestSharp创建对API的请求。

在此请求中,我需要转移.zip文件,int和字符串参数。但是我得到了一个例外状态代码44。我在邮递员中测试了我的方法,它有效。请帮忙。

这是我的API方法所收到的:

[HttpPost]
    [Route("AddFile")]
    public async Task<IActionResult> AddFile(IFormFile file)
    {
        
        int comicsId = Int32.Parse(Request.Form["comicsId"]);
        string issueName = Request.Form["issueName"].ToString();

这是桌面WPF应用程序代码:

private void BtnCreateAdd_OnClick(object sender, RoutedEventArgs e)
{
    if (cbComicsName.SelectedItem == null || tbPath.Text.Trim().Length == 0 || tbIssueName.Text.Trim().Length==0)
    {
        
    }
    else
    {
        var fileParameter = FileParameter.FromFile(tbPath.Text);
        ComboBoxItem comboBoxItem = (ComboBoxItem)cbComicsName.SelectedItem;
            var response = apiClient.Post(new RestRequest("download/addfile")
                .AddParameter("comicsId",Convert.ToInt32(comboBoxItem.Tag))
                .AddParameter("issueName",tbIssueName.Text)
                .AddFile("file",fileParameter.GetFile,fileParameter.FileName,fileParameter.ContentType));
            MessageBox.Show(response.Content);


    }
}

这是错误,

System.Net.Http.HttpRequestException: Request failed with status code 44

这是我使用Postman与其成功代码

”

I have method in my API which receives a file, int and string params. Also I have a desktop WPF app (written in C#) where user after choosing some options click on button and app create request to API using RestSharp.

In this request I need to transfer a .zip file, int and string params. But I get an exception status code 44. I test my method in Postman and it works. Please help.

This is what my API method receives:

[HttpPost]
    [Route("AddFile")]
    public async Task<IActionResult> AddFile(IFormFile file)
    {
        
        int comicsId = Int32.Parse(Request.Form["comicsId"]);
        string issueName = Request.Form["issueName"].ToString();

This is the desktop WPF app code:

private void BtnCreateAdd_OnClick(object sender, RoutedEventArgs e)
{
    if (cbComicsName.SelectedItem == null || tbPath.Text.Trim().Length == 0 || tbIssueName.Text.Trim().Length==0)
    {
        
    }
    else
    {
        var fileParameter = FileParameter.FromFile(tbPath.Text);
        ComboBoxItem comboBoxItem = (ComboBoxItem)cbComicsName.SelectedItem;
            var response = apiClient.Post(new RestRequest("download/addfile")
                .AddParameter("comicsId",Convert.ToInt32(comboBoxItem.Tag))
                .AddParameter("issueName",tbIssueName.Text)
                .AddFile("file",fileParameter.GetFile,fileParameter.FileName,fileParameter.ContentType));
            MessageBox.Show(response.Content);


    }
}

This is error

System.Net.Http.HttpRequestException: Request failed with status code 44

This is the way I use Postman to check method with it successful code

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文