Processing Multipart MIME in ASP.Net Web API

Introduction

This article provides an example of a Multipart MIME in the ASP.NET Web API. Multipurpose Internet Mail Extension (MIME) allows entities to be encapsulated. Multipart MIME is associated with the "Media Types Family".

Procedure of accessing Multipart MIME in the Web API.

Step 1

Create the Web API application.

  • Start Visual Studio 2012 and  select "New Project".
  •  In the template window select "Installed Template" -> "Visual C#" -> "web".
  • Choose application "ASP.NET MVC 4 Web Application".
  • Click on the "OK" button.

    multi.jpg

  • From the MVC 4 Project window select Web API.

    multi1.jpg

Step 2

Add a controller to the project as in the following:

  • In the "Solution Explorer".
  • Right-click on the "Controller" folder then select "Add" -> "Controller".
  • Now open an Add controller window.

    multi2.jpg

  • Change the name of the controller and click on the "OK" button.

Add these namespaces:

  1. using System.IO;  
  2. using System.Threading.Tasks;  
  3. using System.Diagnostics;  
And write this code:
  1. namespace MultipartMIME.Controllers  
  2. {  
  3.     public class SaveController : ApiController  
  4.     {  
  5.         public async Task<HttpResponseMessage> Doc()  
  6.         {  
  7.             if (!Request.Content.IsMimeMultipartContent())  
  8.             {  
  9.                 throw  
  10.                     new HttpResponseException(HttpStatusCode.UnsupportedMediaType);  
  11.             }  
  12.             string info = HttpContext.Current.Server.MapPath("~/App_Data");  
  13.             var generator = new MultipartFormDataStreamProvider(info);  
  14.             try  
  15.             {  
  16.                 StringBuilder str = new StringBuilder();  
  17.                 await Request.Content.ReadAsMultipartAsync(generator);  
  18.                 foreach (var gen in generator.FormData.AllKeys)  
  19.                 {  
  20.                     foreach (var data in generator.FormData.GetValues(gen))  
  21.                     {  
  22.                         str.Append(string.Format("{0}: {1}\n", gen, data));  
  23.                     }  
  24.                 }  
  25.                 return new HttpResponseMessage()  
  26.                 {  
  27.                     Content = new StringContent(str.ToString())  
  28.                 };  
  29.             }  
  30.             catch (System.Exception x)  
  31.             {  
  32.                 return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, x);  
  33.             }  
  34.         }  
  35.     }  
  36. }

The "IsMimeMultipartContent()" method checks the Multipart MIME message, if none then it returns the unsupported media type.

The "ReadAsMultipartAsync" method reads all the Multipart messages and it produces an HttpContent as a result.

Step 3

Open the "Index.cshtml" file.

  • In the "Solution Explorer".

  • Select "View" -> "Home" -> "index.cshtml".

    multi3.jpg


Write this code:

  1. <header>  
  2.     <div class="content-wrapper">  
  3.         <div class="float-left">  
  4.             <p class="site-title">  
  5.                 <a href="~/">ASP.NET Web API</a>  
  6.             </p>  
  7.         </div>  
  8.     </div>  
  9. </header>  
  10. <div id="body">  
  11.     <h2>Using MIME Multipart</h2>  
  12.     <section class="main-content clear-fix">  
  13.         <form name="tour_search" method="post" enctype="multipart/form-data" action="api/save">  
  14.             <fieldset>  
  15.                 <legend>Example train selection</legend>  
  16.             </fieldset>  
  17.             <label for="tour">Tour</label>  
  18.             <div>  
  19.                 <input type="radio" name="tour" value="return-journey" checked="checked"/>  
  20.                Return journey  
  21.             </div>  
  22.             <div>  
  23.                 <input type="radio" name="tour" value="one-way"/>  
  24.                 One-Way  
  25.             </div>  
  26.             <label for="class">Class</label>  
  27.             <div>  
  28.                 <input type="checkbox" name="options" value="Sleeper" />  
  29.                 Sleeper  
  30.             </div>  
  31.             <div>  
  32.                 <input type="checkbox" name="options" value="first-AC" />  
  33.                First AC  
  34.             </div>  
  35.             <div>  
  36.                 <input type="checkbox" name="options" value="Second-AC" />  
  37.                 Second AC  
  38.             </div>  
  39.             <div>  
  40.                 <input type="checkbox" name="options" value="Third-AC" />  
  41.                 Third AC  
  42.             </div>  
  43.             <div>  
  44.                 <label for="seat">Choice if any</label>  
  45.                 <select name="seat">  
  46.                     <option value="select">Select</option>  
  47.                     <option value="upper">Upper</option>  
  48.                     <option value="middle">Middle</option>  
  49.                     <option value="lower">Lower</option>  
  50.                     <option value="No choice">No Choice</option>  
  51.                 </select>  
  52.             </div>  
  53.             <div>  
  54.                 <input type="submit" value="Send" />  
  55.             </div>  
  56.         </form>  
  57.     </section>  
  58. </div>   

Step 4

Execute the application by pressing F5.

multi4.jpg

Select the information.

multi5.jpg

Click on the "Send" button.

multi6.jpg


Similar Articles