andrewlocatelliwoodcock

Thoughts on Software

How to create an ActionResult from a string

with 2 comments


There are times when for whatever reason you want to return a string from a call to an Action method instead of a formal View; perhaps you want to return a Base64 encoded image or display the contents of a file, for example. ASP.Net MVC actually makes this very straightforward to do but it is not that well documented: use the Content method of the Controller as in the example below:

 

public ActionResult GetFileContents(string file)

{

return Content(SomeMethodToReturnFileContentsAsString(file));

}

And that’s all there is to it. Simples!

Written by andrewlocatelliwoodcock

November 18, 2011 at 22:13

Posted in ASP.Net MVC, C#

Tagged with

2 Responses

Subscribe to comments with RSS.

  1. … [Trackback]…

    […] Read More: andrewlocatelliwoodcock.com/2011/11/18/how-to-create-an-actionresult-from-a-string/ […]…

    Elektrische Zahnbuerste

    November 21, 2011 at 23:28

  2. This really is a quite exciting post, Im seeking for this know how. So you recognize I established your internet internet site when I was looking for internet sites like my own, so please appear at my internet internet site someday and post me a opinion to let me know how you feel.

    Elektrische Zahnbuerste

    November 22, 2011 at 00:23


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: