How do I return an HTML controller?

How do I return an HTML controller?

You can use the Content method with the Content-Type text/html to return the HTML directly, without the need of Html. Raw . You can pass whatever Content-Type you want, such text/xml . Use Server.

How do I return HTML in Web API?

  1. If you want to show a html from file, just add “var content = System.IO.File.ReadAllText(“index.html”);”
  2. Yup, if you are using ASP.NET Core 2.0 this is the way to go!
  3. What if the HTML file is in the local directory and it also has css, js linked.

How do I return a view from a controller?

To return a view from the controller action method, we can use View() method by passing respective parameters. return View(“ViewName”) – returns the view name specified in the current view folder (view extension name “. cshtml” is not required. return View(“~/Views/Account/Register.

Can a REST API return HTML?

One of the core benefits of REST is its separation of representation (encoding) from the underlying resource being accessed. It’s perfectly fine to return HTML if the client requests it as a preference via the Accept header.

How do I return a Spring controller in HTML?

Follow below steps:

  1. Must put the html files in resources/templates/
  2. Replace the @RestController with @Controller.
  3. Remove if you are using any view resolvers.
  4. Your controller method should return file name of view without extension like return “index”

How do I redirect in Web API?

redirect() The redirect() method of the Response interface returns a Response resulting in a redirect to the specified URL. Note: This is mainly relevant to the ServiceWorker API. A controlling service worker could intercept a page’s request and redirect it as desired.

Should you use HTML Raw?

Text output will generally be HTML encoded. Using Html. Raw allows you to output text containing html elements to the client, and have them still be rendered as such. Should be used with caution, as it exposes you to cross site scripting vulnerabilities.

How to return HTML from an API controller?

However if your use case is that you need to return HTML from an API Controller for whatever reason, here is what you need to do. Let us say we have the following HTML: If this file is located in a folder Content, we can return its content from a controller using the following:

How to return a HTML page from a restful controller?

If you have already configured ViewResolver of the application returning a simple String will not work and the String will be interpreted as a normal String instead of JSP view name. To overcome these two limitations, you can return a ModelAndView instead. I hope this answer would be helpful, thanks.

How to return plain HTML from a Spring MVC controller?

In this tutorial, we want to have a look at how to return HTML from a Spring MVC controller. Let’s take a look at what needs to be done. 2. Maven Dependency First, we have to add the spring-boot-starter-web Maven dependency for our MVC controller:

How to return contents of a HTML file?

If this file is located in a folder Content, we can return its content from a controller using the following: In the above we get the contents using File.ReadAllText and return it using the ContentResult in ASP.Net core. The result is the following when you visit /index: