MVC ActionResult inheritance

by OfirYaron 7/19/2009 5:47:00 AM

MVC contains a nice class you can inherit inorder to return http responses other then a regular Render View:

bellow are three implementations I use the most:

ImageResult, TextResult and RssResult:

public class ImageResult : ActionResult

{

public Stream ImageStream { get; private set; }

public string ContentType { get; private set; }

public ImageResult(Stream imageStream, string contentType)

{

if (imageStream == null)

throw new ArgumentNullException("imageStream");

if (contentType == null)

throw new ArgumentNullException("contentType");

this.ImageStream = imageStream;this.ContentType = contentType;

}

public override void ExecuteResult(ControllerContext context)

{

if (context == null)

throw new ArgumentNullException("context");

HttpResponseBase response = context.HttpContext.Response;response.ContentType =

this.ContentType;

byte[] buffer = new byte[4096];

while (true)

{

int read = this.ImageStream.Read(buffer, 0, buffer.Length);

if (read == 0)

break;

response.OutputStream.Write(buffer, 0, read);

}

response.End();

}

}

public class TextResult : ActionResult

{

public string Content { get; private set; }
public TextResult(string content)

{

this.Content = content;

}

public override void ExecuteResult(ControllerContext context)

{

if (context == null)

throw new ArgumentNullException("context");HttpResponseBase response = context.HttpContext.Response;

response.Write(Content);

response.End();

}

public class RssResult : ActionResult

{

public SyndicationFeed Feed { get; set; }

public RssResult() { }

public RssResult(SyndicationFeed feed)

{

this.Feed = feed;

}

public override void ExecuteResult(ControllerContext context)

{

context.HttpContext.Response.ContentType =
"application/rss+xml";

Rss20FeedFormatter formatter = new Rss20FeedFormatter(this.Feed);

using (XmlWriter writer = XmlWriter.Create(context.HttpContext.Response.Output))

{

formatter.WriteTo(writer);

}

}

}

Currently rated 5.0 by 2 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , ,

MVC

Where is that ActionLink!?!

by OfirYaron 2/25/2009 3:51:00 AM

I've been wrestling with MVC today.

I recently installed the RC after working with preview version for a while for some reason lots of my most favorite extended methods were gone after this installation.

So far I've tried re-installing the MVC RC package, overriding the dll manually, removing all the other assemblies from the web.config - leaving system.web.mvc the only one and nothing helped.

Oddly enough the object explorer show the ActionLink method on system.web.mvc.dll (namespace: system.web.mvc.html class:LinkExtensions) But all the insallisence give is a a short list that that seem to come from HtmlHelper but not those I need:

After playing with it a little - trying different assemblies I've found that the Extensions were moved to 'system.web.mvc.html', and the reason it didn't work was becouse the GAC was still holding an old version of that assembly (Preview 5)

Currently rated 4.3 by 3 people

  • Currently 4.333333/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: ,

MVC

Powered by BlogEngine.NET 1.3.0.0
Theme by Mads Kristensen

About the author

Name of author Author name
Something about me and what I do.

E-mail me Send mail

Calendar

<<  July 2010  >>
MoTuWeThFrSaSu
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

View posts in large calendar

Recent posts

Recent comments

Authors

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2010

Sign in