Here is my controller i am sending my html
public class MyModuleController : Controller
{
// GET: api/values
[HttpGet]
public HttpResponseMessage Get()
{
var response = new HttpResponseMessage();
response.Content = new StringContent("<html><body>Hello World</body></html>");
response.Content.Headers.ContentType = new MediaTypeHeaderValue("text/html");
return response;
}
}
In response i am getting this
{"version":{"major":1,"minor":1,"build":-1,"revision":-1,
"majorRevision":-1,"minorRevision":-1},"content":{"headers":[{"key":"Content-Type","value":["text/plain;
charset=utf-8"]}]},"statusCode":200,"reasonPhrase":"OK","headers":[],"requestMessage":null,"isSuccessStatusCode":true}
I just want my html in output.Please can anyone help,Thanku
Use ControllerBase. The ControllerBase. Content() method returns a ContentResult object. This method has several overloads, and we will be using an overload that accepts two string parameters. The first string represents the content of the HTML while the last is the content-type which for HTML is "text/html" .
Depending on which of these is returned, Web API uses a different mechanism to create the HTTP response. Convert directly to an HTTP response message. Call ExecuteAsync to create an HttpResponseMessage, then convert to an HTTP response message. Write the serialized return value into the response body; return 200 (OK).
You don't return a View from an API controller. But you can return API data from an MVC controller. The solution would be to correct the errors, not try to hack the API controller.
IActionResult type. The IActionResult return type is appropriate when multiple ActionResult return types are possible in an action. The ActionResult types represent various HTTP status codes. Any non-abstract class deriving from ActionResult qualifies as a valid return type.
Thanku to @genichm and @smoksnes,this is my working solution
public class MyModuleController : Controller
{
// GET: api/values
[HttpGet]
public ContentResult Get()
{
//return View("~/Views/Index.cshtml");
return Content("<html><body>Hello World</body></html>","text/html");
}
}
You can use ContentResult
, which inherits ActionResult
. Just remember to set ContentType
to text/html
.
public class MyModuleController : Controller
{
[HttpGet]
public IActionResult Get()
{
var content = "<html><body><h1>Hello World</h1><p>Some text</p></body></html>";
return new ContentResult()
{
Content = content,
ContentType = "text/html",
};
}
}
It will return a correct Content-Type:
Which will cause the browser to parse it as HTML:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With