Relative URL when host uses virtual directory

Topics: ASP.NET 2.0
Aug 27, 2007 at 7:21 PM
Hi everyone, although I'm aware there is a seperate GoDaddy thread, I'm not quite sure this is related to this host per se. Therefore a quick question:

Apparently, the way multiple websites are hosted over there (and an ASP 2.0 application), it creates a virtual folder. So far so good, until I add entries and browse them on the website, it 'translates' the files to http://www.MyWebSite.com/MyWebsite.com/post/welcome-to-my-site.aspx
(and yes, I have to create and assign a folder for a new ASP.NET application). I could name the folder also Content or Blog or Site whatsoever, but since there are multiple domains and sites running on the single GoDaddy host account, I identified them by using the domain name.

Is there any way to bypass resolving the virtual folder name?

Thanks,
Mike
Aug 29, 2007 at 10:35 AM
GoDaddy just replied, that this is by design how virtual paths/applications/folders is configured for their Windows servers. Decided to use the free plan so all files are in the 'real root'. I had no idea those 'courtesy ad banners' were so obnoxious. Those banners are even displayed when you edit a page and click like 'show/edit html', upload a file/image which completely ruins the GUI (hence making it very hard to save changes).
Aug 31, 2007 at 9:35 PM
You can change it.
I've a shared hosting that had the same problems -> it's showed the path included.

The workaround i've made for this :

  • Step 1 : Create an new enty in the web.config file (root file)
Add new section to the web.config.

Add at the end of the file, just before the </configuration>
Add this :
----------------------------------------------
<appSettings>
<add key="domainname" value="MyWebsite.com" />
</appSettings>
--------------------------------------------
MyWebsite.com --> the virtual directory you have made.

  • Step 2 :
Change the BlogEngine.core part :
- Open the file : posts.cs file.

Search for the property:
public Uri RelativeLink
{

get
{... }
}
-----------------------------------------
Now replace the content of the RelativeLink property to :
----------------------------------------------------------------------------
public Uri RelativeLink
{

get
{
NameValueCollection mySettings = System.Configuration.ConfigurationSettings.AppSettings;
string domainname = mySettings"domainname";
string url = VirtualPathUtility.ToAbsolute("~/post/" + Utils.RemoveIlegalCharacters(Title) + ".aspx");
url = url.Replace("/" + domainname, "");
return new Uri(url, UriKind.Relative);
}
}
----------------------------------------------------------------------------
What it's does, its looks for the domainname and replaces it with empty string.
remark : Make sure that the virtualpath is different from the domainname.
exaple : mysite.com --> make your virtual path to : mysitecom (remove the '.') otherwise it will be removed both..
...

Next step,
Rebuild the BlogEngine.Core,
when done, put the dll's and xml (build maybe in Release mode) and put them under the /bin directory
in your godaddy hosting.


Restart your website apps..
You will now see that the post have the right URL (without the map 'MyWebsite.com')

Regards,
P.



MikevZ wrote:
GoDaddy just replied, that this is by design how virtual paths/applications/folders is configured for their Windows servers. Decided to use the free plan so all files are in the 'real root'. I had no idea those 'courtesy ad banners' were so obnoxious. Those banners are even displayed when you edit a page and click like 'show/edit html', upload a file/image which completely ruins the GUI (hence making it very hard to save changes).

Sep 1, 2007 at 4:50 PM
Thanks Peter for that complete walkthrough. I'll test and use it for some less critial blogs - but I wonder if I should implement it for other sites. I'm afraid I'd have to cross my fingers every time we upgrade to a new version. Thanks again!
Sep 2, 2007 at 1:19 AM
Peter, I've just tested your code. I think it works for posts. All other generated links are still pointing to the virtual application path right? (e.g., categories is still http://www.mydomain.com/mydomaincom/category/general.aspx). Same is for permalinks, the RSS feeds, etc. Pretty intensive work I think to remove all the virtual paths. Am I missing something? Thanks!
Sep 2, 2007 at 1:23 AM
What about updating the Utils.cs? It has for instance:

public static string RelativeWebRoot
{
get { return VirtualPathUtility.ToAbsolute("~/"); }
}

and then:

public static Uri AbsoluteWebRoot
{
get
{
if (_AbsoluteWebRoot == null)
{
HttpContext context = HttpContext.Current;
if (context == null)
throw new NullReferenceException("The current HttpContext is null");

_AbsoluteWebRoot = new Uri(context.Request.Url.Scheme + "://" + context.Request.Url.Authority + RelativeWebRoot);
}
return _AbsoluteWebRoot;
}
}

That must be get us somewhere? I'm not a programmer, while I did try to play with some modifications - but that completely messed up the CSS.