我可以访问global.asax.cs中的虚拟目录名称吗?

发布于 2024-07-18 08:18:36 字数 1027 浏览 4 评论 0原文

属性 HttpContext.Current.Request.ApplicationPath 表示 IIS 或 WebDev.WebServer 中的虚拟目录。

 HttpContext.Current.Request.ApplicationPath evaluates to "/virtualdirectory"

这可以与 VirtualPathUtility 结合使用来使路径根相对:

 VirtualPathUtility.ToAbsolute("~/images/cat.jpg",
                               HttpContext.Current.Request.ApplicationPath)

 // (this evaluates to "/virtualdirectory/images/cat.jpg")

在 IIS6 和 WebDev.WebServer 中,Request 对象global.asax.cs< 中可用。 /code>,但 IIS7 抱怨它“在当前上下文中不可用”。 因此上面的第二行代码可以工作,但在 IIS7 中不行。

问题是我需要访问 global.asax.cs 中的虚拟目录名称。 我需要它来构造一些在动态创建的 CSS 中使用的路径。 有没有其他方法可以访问这个值?

编辑:这是在 IIS 7 中调用 Application_Start 下的 global.asax.cs 中的 HttpContext.Current.Request 时出现的错误:

 HttpException (0x80004005): Request is not available in this context]
    System.Web.HttpContext.get_Request() +8789264

The property HttpContext.Current.Request.ApplicationPath represents the virtual directory in IIS or WebDev.WebServer.

 HttpContext.Current.Request.ApplicationPath evaluates to "/virtualdirectory"

This can be used in conjunction with VirtualPathUtility to make a path root relative :

 VirtualPathUtility.ToAbsolute("~/images/cat.jpg",
                               HttpContext.Current.Request.ApplicationPath)

 // (this evaluates to "/virtualdirectory/images/cat.jpg")

In IIS6 and WebDev.WebServer the Request object is available in global.asax.cs, but IIS7 complains that it is 'not available in current context'. Therefore the second line of code above works but not in IIS7.

The problem is I need to access the virtual directroy name within global.asax.cs. I need it to construct some paths that are used in dynamically created CSS. Is there an alternative way to access this value?

Edit: This is the error you get in IIS 7 for calling HttpContext.Current.Request in global.asax.cs under Application_Start:

 HttpException (0x80004005): Request is not available in this context]
    System.Web.HttpContext.get_Request() +8789264

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

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

发布评论

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

评论(5

断桥再见 2024-07-25 08:18:37

嗯...我不知道 IIS7 的变化。 我想知道推迟此操作是否会更简单,直到您已经获得一个页面。 例如,您可以尝试在 Application_BeginRequestSession_Start 中添加“仅一次”的内容?

或者(完全未经测试)对于自取消订阅挂钩:

    public override void Init() {
        base.Init();
        EventHandler handler = null;
        handler = delegate {
            // do stuff, once only
            this.BeginRequest -= handler;
        };
        this.BeginRequest += handler;
    }

技巧是只执行一次(如果多个请求同时到达); 也许是静态演员? 例如,我认为这只触发一次,并且仅当上下文中存在可用页面时:

    static class DelayedLoader {
        static DelayedLoader() {
            string s = VirtualPathUtility.ToAbsolute("~/images/cat.jpg",
                           HttpContext.Current.Request.ApplicationPath);
        }
        [MethodImpl(MethodImplOptions.NoInlining)]
        public static void Init() { }
    }
    public override void Init() {
        base.Init();
        EventHandler handler = null;
        handler = delegate {
            DelayedLoader.Init();
            this.BeginRequest -= handler;
        };
        this.BeginRequest += handler;
    }

Hmmm... I wasn't aware of the IIS7 change. I wonder if it wouldn't be simpler to defer this operation until you have got a page. For example, you could try putting something "once only" in Application_BeginRequest or Session_Start?

Or (completely untested) for a self-unsubscribing hook:

    public override void Init() {
        base.Init();
        EventHandler handler = null;
        handler = delegate {
            // do stuff, once only
            this.BeginRequest -= handler;
        };
        this.BeginRequest += handler;
    }

The trick is doing it once only (if multiple requests arrive at once); perhaps a static ctor? For example, I think this fires once only, and only when there is a page available in context:

    static class DelayedLoader {
        static DelayedLoader() {
            string s = VirtualPathUtility.ToAbsolute("~/images/cat.jpg",
                           HttpContext.Current.Request.ApplicationPath);
        }
        [MethodImpl(MethodImplOptions.NoInlining)]
        public static void Init() { }
    }
    public override void Init() {
        base.Init();
        EventHandler handler = null;
        handler = delegate {
            DelayedLoader.Init();
            this.BeginRequest -= handler;
        };
        this.BeginRequest += handler;
    }
撑一把青伞 2024-07-25 08:18:37

这是我想出的最好的:Application_BeginRequest(通过标记)

我很少使用asax,以至于我暂时忘记了你会用它得到不同的事件。 到目前为止,我一直在 Application_Start 中创建 CSS 精灵。 将其移至 BeginRequest 是我能想到的最好的办法。

对每个请求进行一次布尔检查可以忽略不计,但如果有不同的方法那就太好了。

  protected void Application_Start()
    {
        RegisterRoutes(RouteTable.Routes);
    }

    protected void Application_BeginRequest()
    {
        if (!_initialized)
        {
            lock (thisLock)
            {
                _initialized = true;
                GenerateCSSSprites();  
            }
        }
    }

This is the best I came up with : Application_BeginRequest (via mark)

I use asax so rarely that I had temporarily forgotten you get different events with it. Until now I'd been creating the CSS sprites in Application_Start. Moving it to BeginRequest was the best I could come up with.

One boolean check for every request is negligible, but would be nice if there is a different way.

  protected void Application_Start()
    {
        RegisterRoutes(RouteTable.Routes);
    }

    protected void Application_BeginRequest()
    {
        if (!_initialized)
        {
            lock (thisLock)
            {
                _initialized = true;
                GenerateCSSSprites();  
            }
        }
    }
Bonjour°[大白 2024-07-25 08:18:37

当切换到 IIS7 时我也遇到了这个问题,但我能够重构对 Request 的需求。 这也是这个人的建议,如果你不能的话,还提供了一个解决方法。

http://mvolo.com/blogs/serverside/archive/2007/11/10/Integrated-mode-Request-is-not-available-in-this-context-in-Application_5F00_Start.aspx< /a>

I had this problem too when switching to IIS7 but I was able to refactor out the need for Request. Which is what this guy also suggests and provides a workaround if you can't.

http://mvolo.com/blogs/serverside/archive/2007/11/10/Integrated-mode-Request-is-not-available-in-this-context-in-Application_5F00_Start.aspx

南城追梦 2024-07-25 08:18:36

终于找到简单的答案了!

 HttpRuntime.AppDomainAppVirtualPath

在 Application_Start 期间立即可用

它的格式为 /myapplication,包括 / 前缀。

Finally found the simple answer!

 HttpRuntime.AppDomainAppVirtualPath

Available immediately during Application_Start

This is of the form /myapplication including the / prefix.

一个人练习一个人 2024-07-25 08:18:36

您可以使用 ResolveUrl("~/images/cat.jpg") 来构建路径吗?

编辑: ResolveUrl 是 Control 的一种方法,而不仅仅是 Page 类,因此您可以这样做(可能有点难看):

System.Web.UI.Control c = new Control();
String s = c.ResolveUrl(@"~/images/cat.jpg");

Can you use ResolveUrl("~/images/cat.jpg") to build your path?

Edit: ResolveUrl is a method of Control, not just the Page class, so you can do it this way instead (bit ugly maybe):

System.Web.UI.Control c = new Control();
String s = c.ResolveUrl(@"~/images/cat.jpg");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文