ihttphandler 完全按原样写出文件

发布于 2024-08-25 09:52:24 字数 370 浏览 2 评论 0原文

我正在编写一个继承自 IHttpHandler 的类,用于脚本和 css 组合。我只想在查询字符串定义了特殊参数时进行组合。如果未定义此参数,那么我想写入文件的内容,就好像处理程序根本不参与一样。完好无损地交付文件的最佳方式是什么?

EIDT:

我遇到的一个问题是,页面上有一个脚本标记,它引用虚拟目录中的脚本,但我所在的页面位于应用程序的子目录中。

引用控制脚本的页面位于 webserver/Admin/Default.aspx。当我访问实现 IHttpHandler 的类中的 Request 对象时,所有文件路径属性如下:webserver/Admin/~/SharedScripts/control.js。我该如何解决这个问题?

I am writing a class that inherits from IHttpHandler for script and css combining. I only want to combine if the querystring has a special parameter defined. If this parameter is not defined then I want to write the contents of the file as if the handler wasn't even involved. What is the best way to deliver the file unharmed?

EIDT:

The one problem I'm encountering is that I have a script tag on the page that refers to a script in a virtual directory but the page I am on is in a subdirectory of the application.

The page that the control script is being referenced from is located at webserver/Admin/Default.aspx. When I access the Request object in the class that implements IHttpHandler all file path properties are the following: webserver/Admin/~/SharedScripts/control.js. How do I resolve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

英雄似剑 2024-09-01 09:52:24

您可以在“ProcessRequest(HttpContext context)”方法中检查查询字符串参数,如下所示:

context.Request.QueryString["paramertername"]

如果您想按原样流式传输请求的文件,则可以执行以下操作:

        string physicalFilePath = context.Request.PhysicalPath;
        string fileContent = string.Empty;

        // Determine whether file exists
        if (File.Exists(physicalFilePath))
        {
            // Read content from file
            using (StreamReader streamReader = File.OpenText(physicalFilePath))
            {
                fileContent = streamReader.ReadToEnd();
            }
        }

        context.Response.Output.Write(convertedFile);
        context.Response.Flush();

PS:您还可以查看以下代码项目文章以获取更全面的示例: http://www.codeproject.com/KB/locale/LocalizedScriptsAndStyles .aspx

You can check the query string parameter in the 'ProcessRequest(HttpContext context)' method like this:

context.Request.QueryString["paramertername"]

If you want to stream the requested file as is you can then do the following:

        string physicalFilePath = context.Request.PhysicalPath;
        string fileContent = string.Empty;

        // Determine whether file exists
        if (File.Exists(physicalFilePath))
        {
            // Read content from file
            using (StreamReader streamReader = File.OpenText(physicalFilePath))
            {
                fileContent = streamReader.ReadToEnd();
            }
        }

        context.Response.Output.Write(convertedFile);
        context.Response.Flush();

PS: You can also check out the following code project article for a more comprehensive example: http://www.codeproject.com/KB/locale/LocalizedScriptsAndStyles.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文