tags:

views:

162

answers:

3

For some strange reasons, i want to write html directly into output stream from Action Method. ( I understand MVC sepearation, but this is a special case. )

Can i write directly into httpresponse output stream ? In that case, which IView object, the Action Method should return ? Can i return 'null' ?

+2  A: 

You can do return Content(...); where, if I remember correctly, ... would be what you want to write directly to the output stream, or nothing at all.

Take a look at the Content methods on the Controller: http://aspnet.codeplex.com/SourceControl/changeset/view/22907#266451

And the ContentResult: http://aspnet.codeplex.com/SourceControl/changeset/view/22907#266450

Jordan S. Jones
A: 

Yes, you can write directly to the Response. After you're done, you can call CompleteRequest() and you shouldn't need to return anything.

For example:

// GET: /Test/Edit/5
public ActionResult Edit(int id)
{

    Response.Write("hi");
    HttpContext.ApplicationInstance.CompleteRequest();

    return View();     // does not execute!
}
womp
You should avoid Response.End() http://stevesmithblog.com/blog/use-httpapplication-completerequest-instead-of-response-end/
John Sheehan
Updated then to use CompleteRequest().
womp
+1  A: 

Write your own Action Result. Here's an example of one of mine:

public class RssResult : ActionResult
{
 public RssFeed RssFeed { get; set; }

 public RssResult(RssFeed feed) {
  RssFeed = feed;
 }

 public override void ExecuteResult(ControllerContext context) {
  context.HttpContext.Response.ContentType = "application/rss+xml";
  SyndicationResourceSaveSettings settings = new SyndicationResourceSaveSettings();
  settings.CharacterEncoding = new UTF8Encoding(false);
  RssFeed.Save(context.HttpContext.Response.OutputStream, settings);
 }
}
John Sheehan