Read IIS7 configuration from within web applicatio

2019-07-27 11:32发布

问题:

Here's what I want to do:

My web application is very slow at first until all pages have been precompiled and the cache has been populated. I want to 'warm up' the application in Global.asax by triggering a few HTTP requests to the slowest pages.

The problem is that I don't know which port the application is running on. Could be 80 (my local IIS or the deployed server, 8080 on our test server or some random port for some colleagues who use the ASP.NET development server)

I thought it would be fairly simple to read the merged web configuration, extract the binding, create a url and trigger the request with HttpWebRequest.

However, I had no luck whatsoever... (I mainly tried WebConfigurationManager)

Any ideas?

回答1:

If you have access to the Page object (I'm not sure if you do in global.asax since I never use it in my applications) then you can use its ResolveUrl(string path) method to get the correct URL eg

string myurl = Page.ResolveUrl("~/MyPage.aspx");

Where ~/ refers to the root of the ASP.Net application



回答2:

I think you should be able to get this info from the Request object.

That said, I would be leery about worrying about this without some hard evidence from production that it is in fact a problem.