在 web.config 处理程序映射中指定应用程序根目录

发布于 2024-10-21 15:24:10 字数 1204 浏览 2 评论 0原文

我有一个带有 HttpHandler 的应用程序,用于处理对 .js 文件的任何请求。我只希望此处理程序处理应用程序根目录中请求的 *.js 文件。

处理程序映射如下所示:

<add name="HandleJS" path="*.js" verb="*" type="MyApp.JsHandler" resourceType="Unspecified" requireAccess="Script" preCondition="integratedMode,runtimeVersionv4.0" />

目前,此处理程序处理所有请求的 *.js 文件。这是我想要的行为的示例。

该请求将由处理程序处理:
http://localhost/myapps/approot/script.js

而这个不会 由处理程序处理:
http://localhost/myapps/approot/dontProcessMe/script.js

我真的希望避免在处理程序路径中包含完整的绝对路径,因此我首先尝试了其他一些操作。

add 元素的 path 属性看起来不允许使用 ~/ 应用程序根机制,因此设置 path="~/*.js" 不起作用。

我还尝试复制内置于 IIS 中的 StaticFile 处理程序并执行以下操作:

<add name="MyStaticFiles" path="*/*.js" verb="*" modules="StaticFileModule" />

<add name="MyStaticFiles" path="dontProcessMe/*.js" verb="*" modules="StaticFileModule" />

两者都只返回 HTTP 状态为 200 的空白响应。

帮助我 Obi Wan Kenobi,你是我唯一的希望。

I have an application with an HttpHandler that processes any requests for a .js file. I only want this handler to process *.js files that are requested in the root of the application.

The handler mapping looks like this:

<add name="HandleJS" path="*.js" verb="*" type="MyApp.JsHandler" resourceType="Unspecified" requireAccess="Script" preCondition="integratedMode,runtimeVersionv4.0" />

Currently, this handler processes ALL requested *.js files. Here is an example of the behavior I want.

This request would be processed by the handler:
http://localhost/myapps/approot/script.js

and this one would not be processed by the handler:
http://localhost/myapps/approot/dontProcessMe/script.js

I'd really like to avoid including the full absolute path in the handler path so I tried some other things first.

It doesn't look like the path property of the add element allows the use of the ~/ application root mechanism, so setting path="~/*.js" doesn't work.

I've also tried replicating the StaticFile handler that's built into IIS and doing something like this:

<add name="MyStaticFiles" path="*/*.js" verb="*" modules="StaticFileModule" />

or

<add name="MyStaticFiles" path="dontProcessMe/*.js" verb="*" modules="StaticFileModule" />

Both of which just return a blank response with an HTTP status of 200.

Help me Obi Wan Kenobi, you're my only hope.

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

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

发布评论

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

评论(2

痞味浪人 2024-10-28 15:24:10

如果不需要处理,可以检查处理程序本身中的 RequestURL 返回实际文件吗?

public void ProcessRequest(HttpContext context)
{
    var applicationPath = context.Request.ApplicationPath;
    var pathAndQuery = context.Request.Url.PathAndQuery;

    var appRelativePath = pathAndQuery.Replace(applicationPath, "");

    var basePath = VirtualPathUtility.GetDirectory(appRelativePath);

    if (basePath != "/")
    {
        // load and return actual file
    }
    else
    {
        // custom logic
    }
}

May be inspect the RequestURL in the handler itself return the actual file if there is no processing needed?

public void ProcessRequest(HttpContext context)
{
    var applicationPath = context.Request.ApplicationPath;
    var pathAndQuery = context.Request.Url.PathAndQuery;

    var appRelativePath = pathAndQuery.Replace(applicationPath, "");

    var basePath = VirtualPathUtility.GetDirectory(appRelativePath);

    if (basePath != "/")
    {
        // load and return actual file
    }
    else
    {
        // custom logic
    }
}
明月夜 2024-10-28 15:24:10

path="/*.js" 上没有骰子?

默认情况下它应该使用相对路径...

No dice on path="/*.js" ?

It should be working with relative paths by default...

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