简体   繁体   English

如何“克隆”传入的ASP.NET MVC Multipart请求以发送到Web API控制器

[英]How to 'clone' an incoming ASP.NET MVC Multipart request to send to a Web API controller

Without performing a Server.TransferRequest I need a way to send the multipart body of an ASP.NET MVC Controller Request to an ASP.NET Web API controller which parses that multipart data. 在不执行Server.TransferRequest的情况下,我需要一种将ASP.NET MVC控制器请求的多部分主体发送到解析该多部分数据的ASP.NET Web API控制器的方法。 I would like to do an HttpClient PostAsync to the API passing along the multipart form data. 我想对API进行HttpClient PostAsync传递,并传递多部分表单数据。

In a nutshell, the purpose of the API controller is to handle generic models and serialize them into key/value pairs. 简而言之,API控制器的目的是处理通用模型并将其序列化为键/值对。 I want to use the MVC controllers to validate various models before sending the request on to the local API Controller. 我想使用MVC控制器来验证各种模型,然后再将请求发送到本地API控制器。 I do not want to call the API directly from the form. 我不想直接从表单中调用API。

Here are the steps: 步骤如下:

  1. User enters data into a form on a page (/Home/Index) 用户将数据输入页面上的表单中(/ Home / Index)
  2. Index controller validates the model being passed in. 索引控制器验证传入的模型。
  3. If valid, the Index controller performs an HttpClient POST to /api/Forms/Submit 如果有效,则索引控制器执行HttpClient POST到/ api / Forms / Submit
  4. The API controller accepts the multipart data and converts it to key value pairs and stores the data in the DB. API控制器接受多部分数据并将其转换为键值对,并将数据存储在DB中。
  5. The API controller returns HttpResponseMessage w/response model. API控制器返回带有响应模型的HttpResponseMessage。

I cannot read a Request.Content object on the MVC controller like I can on the API controller. 我无法像在API控制器上那样读取MVC控制器上的Request.Content对象。 What is the best way to 're-create' the multipart request? “重新创建”多部分请求的最佳方法是什么?

In your MVC controller action after validating the model you can access the raw underlying request stream then pass the stream directly into an HttpWebRequest like so... 在验证模型后,在您的MVC控制器操作中,您可以访问原始的基础请求流,然后将流直接传递到HttpWebRequest中,如下所示:

[HttpPost]
public ActionResult Index(MyModel m)
{
    Request.InputStream.Position = 0;

    //the incoming request stream
    var requestStream = HttpContext.Request.InputStream;

    //the outgoing web request
    var webRequest = (HttpWebRequest)WebRequest.Create("http://yaddayadda/api/TargetApiController/Target");

    Stream webStream = null;

    try
    {
        //copy incoming request body to outgoing request
        if (requestStream != null && requestStream.Length > 0)
        {
            webRequest.Method = "POST";
            webRequest.ContentLength = requestStream.Length;
            webRequest.ContentType = HttpContext.Request.ContentType; // <- included for multipart form content
            webStream = webRequest.GetRequestStream();
            requestStream.CopyTo(webStream);
            webStream.Close();
        }
    }
    finally
    {
        if (null != webStream)
        {
            webStream.Flush();
            webStream.Close();    // might need additional exception handling here
        }
    }

    using (HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse())
    {
        var result = response.StatusCode;
    }


    return View();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我向 Web API Controller 中的 Z9E0DA8438E1E38A8DDZCE7C30 中的 Web 发送多部分请求时使用哪种格式化程序? - Which formatter is used when I send a multipart request to a Web API Controller in ASP.NET CORE? 如何将请求从Asp.net MVC发送到multipart / form-data类型的外部api - how to send request from Asp.net MVC to external api in multipart/form-data type 如何使用Alamofire POST请求将图像作为Base64String发送以及如何在Asp.net MVC4 web Api控制器中处理请求? - How to Send Image as Base64String using Alamofire POST request and how to handle the request in Asp.net MVC4 web Api Controller? 读取ASP.NET WEB API中的每个传入请求(URL) - Reading every incoming request (URL) in ASP.NET WEB API 如何在ASP.NET MVC的控制器中获取传入的InputStream - How to get incoming InputStream, in controller in ASP.NET MVC 如何发送 multipart/form-data 到 ASP.NET Core Web API? - How to send multipart/form-data to ASP.NET Core Web API? 如何在同一项目中的ASP.NET MVC控制器中从ASP.NET Web API获取数据 - How get data from asp.net web api in asp.net mvc controller in the same project 如何从 ASP.NET 核心 mvc 向 ASP.NET 核心中的 Web API 发出 PUT 请求? - How to make a PUT request from ASP.NET core mvc to Web API in asp.net core? 将数据从Web API控制器发送到单独的ASP.NET MVC网站 - Send data from web API controller into separate asp.net mvc website 如何发送ajax请求以请求XML响应到asp.net MVC 4控制器? - How to send ajax request to request XML response to asp.net MVC 4 controller?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM