Вопрос

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?

Это было полезно?

Решение

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.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top