ASP.Net MVC的ActionLink的和虚拟主机别名域 [英] ASP.Net MVC ActionLink's and Shared Hosting Aliased Domains

查看:95
本文介绍了ASP.Net MVC的ActionLink的和虚拟主机别名域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我读过<一个href=\"http://stackoverflow.com/questions/364637/asp-net-mvc-on-godaddy-not-working-not-primary-domain-deployment\">this和我有一个类似的问题:

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

我有一个共享的托管帐户(与GoDaddy,尽管这不完全相关的,我相信),我已经有了部署到子文件夹而在那里我有另一个域名映射到一个MVC(RC1),网站(或别名)。子文件夹也设置为应用程序根为好。

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.

该网站工作没有问题,问题是我不喜欢正在使用Html.ActionLink和Ajax.ActionLink生成的链接。它插入子文件夹名称作为URL的一部分,在这个<一个描述href=\"http://stackoverflow.com/questions/364637/asp-net-mvc-on-godaddy-not-working-not-primary-domain-deployment\">other问题。事情是,该网站工作正常,但我想,以相对于该域名生成的链接。

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.

要使用一个例子:

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

这是我的生成的链接 xyz.com 是这样的:

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

和,FWIW,该网站的作品。

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.

推荐答案

我在这个遍地看着,不能拿出任何东西,除了这是一些托管服务提供商的一个副产品。通过MVC堆栈向下钻取,我发现ActionLinks最终通过获取途径虚拟路径和$ P $与Htt的prequestContext.ApplicationPath pfixing它创造。

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.

在GoDaddy的托管(及其他),即使你已经定义了一个域映射到一个已经映射域之下的某个文件夹路径,即在Htt的prequestContext最终应用路径是,如果它是你的根域。所以,真正的东西时髦是怎么回事那里。这是如果打开跟踪并查看使用的trace.axd一个请求的HTTP变量证明。在我给了以上问题的例子中,HTTP_HOST将被列为 my.xyz.com 为非主要领域。然而,即使我在这个域的根访问的东西,在SCRIPT_NAME,PATH_INFO和URL变量都是 / _ xyz.com / 。这最终传播到由AjaxHelper(和的HtmlHelper)ActionLink的扩展方法产生的链接。

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.

所以,我总黑客如下:

 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;
    }
  }

这是应用为这样的:

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.

这篇关于ASP.Net MVC的ActionLink的和虚拟主机别名域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆