You're disposing the stream immediately after the return because of the using statement... So you're returning a closed stream to be read which will undoubtedly throw an unhandled exception.
Create FileStreamResultfrom HttpHandler
-
01-06-2022 - |
Вопрос
I'm trying to hack into an external HttpHandler to return a FileStreamResult in my MVC application. I want to be able to have a clickable link and I'd rather not use JavaScript.
The path is correct, I can call the path directly in the browser; I just get the following error:
The request was aborted: The connection was closed unexpectedly.
I can call the path directly in the browser.
My code so far:
public FileStreamResult Test()
{
Uri uri = this.ControllerContext.HttpContext.Request.Url;
string leftPart = uri.GetLeftPart(UriPartial.Authority);
Uri cssUri = new Uri(leftPart + "/css.axd?path=test.css");
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(cssUri);
using (Stream stream = request.GetResponse().GetResponseStream())
{
return new FileStreamResult(stream, "text/css")
{
FileDownloadName = "test.css"
};
}
}
Does anyone have any pointers they can give me to show where I'm going wrong?
Решение
Не связан с StackOverflow