Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In MVC, how do I return a string result?

In my AJAX call, I want to return a string value back to the calling page.

Should I use ActionResult or just return a string?

like image 212
user67033 Avatar asked Feb 16 '09 17:02

user67033


People also ask

How do you return a string in HTML?

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 .

What is return content in MVC?

ContentResult return type is used for returning Content i.e. String, XML string, etc. from Controller to View in ASP.Net MVC Razor. Controller. The Controller consists of two Action methods. Action method for handling GET operation.

What does return View () in MVC do?

This process determines which view file is used based on the view name. The default behavior of the View method ( return View(); ) is to return a view with the same name as the action method from which it's called.


1 Answers

You can just use the ContentResult to return a plain string:

public ActionResult Temp() {     return Content("Hi there!"); } 

ContentResult by default returns a text/plain as its contentType. This is overloadable so you can also do:

return Content("<xml>This is poorly formatted xml.</xml>", "text/xml"); 
like image 135
swilliams Avatar answered Oct 16 '22 14:10

swilliams