-1

I have a code here which actually converts HTML to PDF and sends it to an email but it is in ActionResult:

public ActionResult Index()
{
    ViewBag.Title = "Home Page";

    var coverHtml = RenderRazorViewToString("~/Views/Home/Test.cshtml", null);
    var htmlContent = RenderRazorViewToString( "~/Views/Home/Test2.cshtml", null);
    string path = HttpContext.Server.MapPath("~/Content/PDF/html-string.pdf");
    PDFGenerator.CreatePdf(coverHtml, htmlContent, path);


    //PDFGenerator.CreatePdfFromURL("https://www.google.com", path);
    EmailHelper.SendMail("myemail@domain.com", "Test", "HAHA", path);

    return View();
}

I want to turn this into a api format (api/SendPDF) using POST with the content ID and email address which it will be sent to, but I am not sure how to do it since I am very new to MVC and Web API. Appreciate some help on this.

Poosh
  • 532
  • 2
  • 10
  • 25

2 Answers2

1

First create a class eg. Information.cs

public class Information{
    public int ContentId {get; set;}
    public string Email {get; set;}
}

In API Controller,

[HttpPost]
public HttpResponseMessage PostSendPdf(Information info)
{
    // Your email sending mechanism, Use info object where you need, for example, info.Email
    var coverHtml = RenderRazorViewToString("~/Views/Home/Test.cshtml", null);
    var htmlContent = RenderRazorViewToString( "~/Views/Home/Test2.cshtml", null);
    string path = HttpContext.Server.MapPath("~/Content/PDF/html-string.pdf");
    PDFGenerator.CreatePdf(coverHtml, htmlContent, path);


    //PDFGenerator.CreatePdfFromURL("https://www.google.com", path);
    EmailHelper.SendMail(info.Email, "Test", "HAHA", path);


    HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, products);
    return response;
}
Abdur Rahim
  • 520
  • 8
  • 18
1

You will probably want to create an ApiController (it looks like you are implementing Controller from System.Web.Mvc. Make sure you included Web API in your project.

I use the following model in my examples:

public class ReportModel
{
    public string ContentId { get; set; }
    public string Email { get; set; }
}

Here is an example ApiController for sending your PDF:

public class SendPDFController : ApiController
{
    [HttpPost]
    public HttpResponseMessage Post([FromUri]ReportModel reportModel)
    {
        //Perform Logic
        return Request.CreateResponse(System.Net.HttpStatusCode.OK, reportModel);
    }
}

This allows you to pass the parameters in the URI, in this case http://localhost/api/SendPDF?contentId=123&email=someone@example.com. This format will work with the default route that Visual Studio includes in the WebApiConfig:

 config.Routes.MapHttpRoute(
    name: "DefaultApi",
    routeTemplate: "api/{controller}/{id}",
    defaults: new { id = RouteParameter.Optional }
);

You can also pass the parameters in the body of your request. You would alter your Post method like so:

[HttpPost]
public HttpResponseMessage Post([FromBody]ReportModel reportModel)
{
    //Perform Logic
    return Request.CreateResponse(HttpStatusCode.OK, reportModel);
}

Then your request URI would be http://localhost/api/SendPDF, Content-Type header as application/json, and Body:

{
    "ContentId": "124",
    "Email": "someone@example.com"
}

If you pass your parameters in the body, the JSON request has been serialized into your model for you, so you can access the parameters you need for the report from the reportModel object within your method.

Poosh
  • 532
  • 2
  • 10
  • 25