Click here to Skip to main content
15,884,353 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

i want to return html code from web api by using IhttpActionResult


public IHttpActionResult Get()
{
string Filepath = HttpContext.Current.Request.MapPath(@"~/Template1/Receipt1.html");
string Html = File.ReadAllText(Filepath);
StringBuilder sb = new StringBuilder(Html);

// what code should i write here
return Ok();
}

What I have tried:

i have done

public HttpResponseMessage Get()
{
string Filepath = HttpContext.Current.Request.MapPath(@"~/Template1/Receipt1.html");
string Html = File.ReadAllText(Filepath);
StringBuilder sb = new StringBuilder(Html);
var response = new HttpResponseMessage();
response.Content = new StringContent(sb.ToString());
response.Content.Headers.ContentType = new MediaTypeHeaderValue("text/html");
response.ReasonPhrase = "success";
return response;
}

above is working fine but i want to use IHttpActionResult

public IHttpActionResult Get()
{
string Filepath = HttpContext.Current.Request.MapPath(@"~/Template1/Receipt1.html");
string Html = File.ReadAllText(Filepath);
StringBuilder sb = new StringBuilder(Html);

// what code should i write here
return Ok();
}
Posted
Updated 22-Mar-19 21:05pm
Comments
Nathan Minier 22-Mar-19 7:09am    
My advice? Don't ever do this.

The entire point of using a web service is to provide concise, data-only responses to an arbitrary client. Your web service shouldn't know what sort of client is consuming it, so sending formatted HTML instantly breaks this concept. Send raw data, and format it on the client side.
Murph1329 17-Mar-21 12:51pm    
This is exactly what MVC does. MVC is just endpoints that return raw HTML instead of JSON or XML. Literally that's the only difference. It makes it easier for the programmer by leveraging some helper libraries to make it seamless. In fact you can call MVC web-methods via ajax and append them to your DOM using jQuery. You can make a SPA using jQuery and MVC. In fact I think it's better because you can lazy load assets instead of forcing the user knowing everything about the application on initial load (angular).

The answer to the OP is use MVC to simply it. With MVC you can even control the response schema, JSON/HTML.

OP, check out the solution. It's really neat but it's kind of silly. It's recreating MVC while inheriting APIController instead of System.MVC.Controller. The only use case I think of for this is someone that has a legacy app and is unable to leverage the MVC references.
F-ES Sitecore 22-Mar-19 8:47am    
Look here for possible solutions

https://docs.microsoft.com/en-us/aspnet/web-api/overview/getting-started-with-aspnet-web-api/action-results

As already suggested though, what you're doing is breaking the whole concept of web apis.

1 solution

 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900