Действительно ли я могу получить доступ к имени виртуального каталога в global.asax.cs?

Входные данные с (name = "amount" & amp; name = "item_name"), их значения должны быть заключены следующим образом:

value ="php code inside"
7
задан Simon_Weaver 29 April 2009 в 08:41
поделиться

5 ответов

Наконец-то нашел простой ответ!

 HttpRuntime.AppDomainAppVirtualPath

Доступно непосредственно при запуске приложения

Это форма /myapplication, включающая префикс /.

.
14
ответ дан 6 December 2019 в 15:31
поделиться

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");
0
ответ дан 6 December 2019 в 15:31
поделиться

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();  
            }
        }
    }
0
ответ дан 6 December 2019 в 15:31
поделиться

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

0
ответ дан 6 December 2019 в 15:31
поделиться

Хммм ... Я не знал об изменении IIS7. Интересно, не будет ли проще отложить эту операцию, пока у вас не появится страница? Например, вы можете попытаться поместить что-то «только один раз» в Application_BeginRequest или Session_Start ?

Или (полностью не проверено) для ловушки для отказа от подписки:

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

Хитрость делать это только один раз (если одновременно поступает несколько запросов); возможно статический ctor? Например, я думаю, что это срабатывает только один раз, и только когда есть доступная страница в контексте:

    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;
    }
0
ответ дан 6 December 2019 в 15:31
поделиться
Другие вопросы по тегам:

Похожие вопросы: