Запись в поток вывода из действия
По каким-то странным причинам я хочу записать HTML-код прямо в поток ответа из действия контроллера. (Я понимаю разделение MVC, но это особый случай.)
Могу ли я написать прямо в HttpResponse
поток? В том случае, который IView
объект должен вернуть действие контроллера? Могу ли я вернуть "ноль"?
5 ответов
Я использовал класс, полученный из FileResult
чтобы добиться этого, используя обычный шаблон MVC:
/// <summary>
/// MVC action result that generates the file content using a delegate that writes the content directly to the output stream.
/// </summary>
public class FileGeneratingResult : FileResult
{
/// <summary>
/// The delegate that will generate the file content.
/// </summary>
private readonly Action<System.IO.Stream> content;
private readonly bool bufferOutput;
/// <summary>
/// Initializes a new instance of the <see cref="FileGeneratingResult" /> class.
/// </summary>
/// <param name="fileName">Name of the file.</param>
/// <param name="contentType">Type of the content.</param>
/// <param name="content">Delegate with Stream parameter. This is the stream to which content should be written.</param>
/// <param name="bufferOutput">use output buffering. Set to false for large files to prevent OutOfMemoryException.</param>
public FileGeneratingResult(string fileName, string contentType, Action<System.IO.Stream> content,bool bufferOutput=true)
: base(contentType)
{
if (content == null)
throw new ArgumentNullException("content");
this.content = content;
this.bufferOutput = bufferOutput;
FileDownloadName = fileName;
}
/// <summary>
/// Writes the file to the response.
/// </summary>
/// <param name="response">The response object.</param>
protected override void WriteFile(System.Web.HttpResponseBase response)
{
response.Buffer = bufferOutput;
content(response.OutputStream);
}
}
Метод контроллера теперь будет выглядеть так:
public ActionResult Export(int id)
{
return new FileGeneratingResult(id + ".csv", "text/csv",
stream => this.GenerateExportFile(id, stream));
}
public void GenerateExportFile(int id, Stream stream)
{
stream.Write(/**/);
}
Обратите внимание, что если буферизация отключена,
stream.Write(/**/);
становится очень медленным Решение состоит в том, чтобы использовать BufferedStream. Это позволило повысить производительность примерно в 100 раз в одном случае. Увидеть
Да, вы можете написать прямо в ответ. После того, как вы закончите, вы можете вызвать CompleteRequest(), и вам не нужно ничего возвращать.
Например:
// GET: /Test/Edit/5
public ActionResult Edit(int id)
{
Response.Write("hi");
HttpContext.ApplicationInstance.CompleteRequest();
return View(); // does not execute!
}
Если вы не хотите получать свой собственный тип результата, вы можете просто написать Response.OutputStream
и вернуться new EmptyResult()
,
Напишите свой собственный результат действий. Вот пример одного из моих:
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);
}
}
Ты можешь сделать return Content(...);
где, если я правильно помню, ...
будет то, что вы хотите записать непосредственно в выходной поток, или вообще ничего.
Посмотрите на Content
методы на Controller
: http://aspnet.codeplex.com/SourceControl/changeset/view/22907
И ContentResult
: http://aspnet.codeplex.com/SourceControl/changeset/view/22907