开发者

.NET - Dynamically get Web Root URL of another project

I have 4 WebForms projects in my solution. I want to be able to do URL redirects to a page in another WebForms project.

Currently I have to set a URL/port number in my project web settings, set that URL in开发者_JS百科 my web config as an app setting, then read it and handle redirecting.

This is a real pain and makes deployments to various environments obnoxious. Is there a way to dynamically handle this via code?

To make the above even more complication I may have all projects mapped in IIS as such:

  • www.mydomain.com/project1
  • www.mydomain.com/test/project2
  • www.mydomain.com/test/project3

But, that shouldn't matter because you can do this to get the Web Root Url for the server that I want to redirect to:

HttpContext.Current.Request.ApplicationPath;

I am not sure if just handling the web config route is my best option or if I can do this dynamically?

Thanks for any assistance.


This is a pretty standard pattern here and believe it or not, convention and configuration are the ways to handle this -- IIS can lie, or IIS might not know what you want it to do and can easily give you the wrong answer. So, as I was saying:

  • Convention: got some sort of standard relationship for urls within your app? Especially with regards to dev, CI and QA which are typically the frequent deployments? If so, setup a default convention there so that your app always thinks the right way. Typically the best or at least easiest to handle is to have all the different apps under IIS applications off a shared root.

  • Configuration: usually for production use where you might have url/ssl/other things at play. Make all the folders configurable.

Trick to mining the stuff out of IIS, besides the fact it often doesnt give you the answer you want, is that you've got alot of environmental stuff to work through. Presuming your app can get the permissions to query the metabase, you still need to know the idea of the virtual site, etc. It is actually easier to explicitly specify the URL than to figure it out.


http://www.gotknowhow.com/articles/how-to-get-base-url-in-aspnet

public static string BaseSiteUrl
    {
        get
        {
            HttpContext context = HttpContext.Current;
            string baseUrl = context.Request.Url.Scheme + "://" + context.Request.Url.Authority + context.Request.ApplicationPath.TrimEnd('/') + '/';
            return baseUrl;
        }
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜