Writing to Output Stream from Action

前端 未结 5 725
悲&欢浪女
悲&欢浪女 2020-11-29 04:13

For some strange reasons, I want to write HTML directly to the Response stream from a controller action. (I understand MVC separation, but this is a special case.)

C

5条回答
  •  孤独总比滥情好
    2020-11-29 04:41

    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);
        }
    }
    

提交回复
热议问题