views:

802

answers:

7

So, I've read this and I've got a similar issue:

I have a shared hosting account (with GoDaddy, though that's not exactly relevant, I believe) and I've got an MVC (RC1) site deployed to a sub-folder which where I have another domain name mapped (or aliased). The sub-folder is also setup as an application root as well.

The site works without issue, the problem is that I don't like the links that are being generated using Html.ActionLink and Ajax.ActionLink. It's inserting the sub folder name as part of the URL as described in this other question. Thing is, the site works fine, but I'd like to make the links generated relative to the domain name.

To use an example:

http://my.abc.com    "primary" domain; maps to \ on file system
http://my.xyz.com    setup to map to \_xyz.com folder on file system

My generated links on xyz.com look like this:

Intended                              Generated
--------                              ---------
http://my.xyz.com/Ctrller/Action/52   http://my.xyz.com/_xyz.com/Ctrller/Action/52

and, FWIW, the site works.

So, the question: Is there something I can do to get rid of that folder name in the links being generated? I have a couple of brute force ideas, but they aren't too elegant.

+1  A: 

This is probably an obvious question but did you make _abc an app root in IIS?

Ian Suttle
Yes, I did. That's relevant info and I'll add it to the question. Thanks.
Peter Meyer
+2  A: 

I've looked at this over and over and can't come up with anything except that this is a by-product of some hosting providers. Drilling down through the MVC stack, I found that ActionLinks are ultimately created by getting the route virtual path and prefixing it with the HttpRequestContext.ApplicationPath.

On GoDaddy hosting (and others), even though you've defined a domain as mapping to a certain folder path beneath an already mapped domain, the application path that ends up in the HttpRequestContext is the path as if it were on your root domain. So, something real funky is going on there. This is evidenced if you turn on Tracing and look at the HTTP variables for a request using Trace.axd. In the example I gave in the question above, the HTTP_HOST would be listed as my.xyz.com for that "non-primary" domain. However, even though I am accessing something at the root of this domain, the SCRIPT_NAME, PATH_INFO and URL variables are all /_xyz.com/. This ends up propagating up to the links generated by the AjaxHelper (and HtmlHelper) ActionLink extension methods.

So, my total hack is as follows:

 public static class FixAliasedLinkHack {
    public static string RemoveAlias(this string link, string alias) {
      // Find the href param and replace ...
      return link.Replace(alias, string.Empty);
    }

    public static string AutoRemoveAlias(this string link) {
      var appRoot = HttpContext.Current.Request.ApplicationPath;
      return appRoot != "/" ? link.RemoveAlias(appRoot) : link;
    }
  }

This is applied as such:

Ajax.ActionLink(...).AutoRemoveAlias();

Lame? Absolutely...

I am going to leave this question open in hopes that somebody has a more elegant solution I'm just missing.

Peter Meyer
Acecpting my own answer ... though if someone stumbles accross this and has a real solution, I would readily change it.
Peter Meyer
Excellent hack! I've been looking for a while for this answer and I even posted a similar question on SO. 1 vote up.
vbocan
+1  A: 

I'm having the same issue on GoDaddy, yet my site breaks as soon as I do some ajax calls since in my javascript I'm just calling hardcoded URLs ("/Controller/Action").

This same issue is being discussed here:

http://stackoverflow.com/questions/364637/asp-net-mvc-on-godaddy-not-working-not-primary-domain-deployment

Phil Derksen
Hey, thanks for the info.
Peter Meyer
+1  A: 

Has anyone seen a better solution to this? I have the same problem with GoDaddy.

Eric
+1  A: 

I also have the same problem.

Jeremy
+3  A: 
vincentw56
This is an excellent solution. It's even more elegant than the original AutoRemoveAlias() stuff. Thanks a lot for sharing.
vbocan
A: 

How can I compile the system.web.mvc DLL without breaking all my site references ???

Cristovao Morgado