Are you still remember about the article that has told you about how to produce output to the web page using JavaScript Action Method in ASP.NET MVC. To continue the article, today I'm gonna tell you how to produce output to the web page from controller using the last 3 action methods: Content (), Json (), File () Action Method).

Content() Action Method

Content() method renders the string that is passed as parameter to the browser.

public ContentResult OutputContent()
        {
            return Content("This is content.");
        }

When above controller method is called, it simply render "This is content." in the browser. This method returns ContentResult action result that derives from ActionResult.

Json() Action Method

Json() method returns Json string to the browser. Its return type is JsonResult that derives from ActionResult.

public JsonResult OutputToJson()
        {
            UserNamePasswordModel model = new UserNamePasswordModel()
            {
                Password = "Password1",
                UserName = "UserName1"
            };
            return Json(model, JsonRequestBehavior.AllowGet);
        }

// OUTPUT: {"UserName":"UserName1","Password":"Password1"}

The above code converts the UserNamePasswordModel to the Json string and returns to the browser.

File() Action Method

File() action method returns the content of a file to the browser. The return type of this method is FileResult that again derives from ActionResult.

public FileResult OutputFile()
        {
            string path = Server.MapPath("~/Views/Home/About.cshtml");
            return File(path, "text/html");
        }


Hope this article was helpful. Thanks for reading and keep visiting for more ASP.NET MVC related articles.

HostForLIFE.eu ASP.NET MVC 6 Hosting
HostForLIFE.eu is European Windows Hosting Provider which focuses on Windows Platform only. We deliver on-demand hosting solutions including Shared hosting, Reseller Hosting, Cloud Hosting, Dedicated Servers, and IT as a Service for companies of all sizes. We have customers from around the globe, spread across every continent. We serve the hosting needs of the business and professional, government and nonprofit, entertainment and personal use market segments.