Search code examples
restasp.net-web-apimailmessage

web api with multiple complex parameters


I am trying to create an email service web api with multiple attachments. Need to have only one parameter for web api. But, right now I have 2 complex parameters, which web api doesn't accept. Please suggest how do I implement multiple attachments and use only one complex parameter for web api.

[HttpPost]          
[ActionName("sendemail")]
public IHttpActionResult processEmail(EmailModel emailModel, 
List<HttpPostedFileBase> attachments)
{
 ........
}

EmailModel

 public class EmailModel
    {  
        public string ToAddress { get; set; }
        public string FromAddress { get; set; }       
        public string Body { get; set; }

    } 

And, in Email Controller I use the list to attach the attachments to MailMessage object.

 foreach (HttpPostedFileBase attachment in attachments)
   {                
            if (attachment != null)
            {
                    string fileName = Path.GetFileName(attachment.FileName);                   

                    //create linked resource for embedding image
                    LinkedResource pic = new LinkedResource(attachment.InputStream, "image/jpg");                    

                    //add linked resource to appropriate view  
                    htmlView.LinkedResources.Add(pic);
             }

     }

      //add view
      msg.AlternateViews.Add(htmlView);  

Solution

  • One solution would be to accept your email model as your parameter, and grab the uploaded files in the HttpContext.Request.Files to loop over. That's assuming you are posting with multipart/form-data