Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC Core WebAPI project not returning html

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

like image 880
Parshuram Kalvikatte Avatar asked Nov 24 '16 07:11

Parshuram Kalvikatte


People also ask

How do I return HTML content in Web API?

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" .

How do I return a Web API result?

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).

Can Web API return view in MVC?

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.

What does IActionResult return?

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.


2 Answers

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");
            }
  }
like image 132
Parshuram Kalvikatte Avatar answered Oct 12 '22 22:10

Parshuram Kalvikatte


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:

enter image description here

Which will cause the browser to parse it as HTML:

enter image description here

like image 43
smoksnes Avatar answered Oct 12 '22 22:10

smoksnes