URL 转发+屏蔽触发 Url.Content() + 的双根目录ASP.NET MVC 3 RC 下的 Html.Action()

发布于 2024-10-03 02:56:55 字数 1733 浏览 0 评论 0原文

(注意:尝试寻找类似的问题,但最接近的问题似乎仍然与我的问题不匹配。即 MVC - 应用程序根目录在使用 Url.Content/Url.Action 的 url 中出现两次

我目前有两个正在运行的 ASP.NET MVC 3 RC 应用程序在一个域下,即

mydomain.com/app1

mydomain.com/app2

现在,因为它是一个单独的应用程序,所以我注册了另一个域名并启用了掩码转发到其中一个应用程序 (即 http://foo.com 将加载 http://mydomain.com/app1 并且在浏览器中,只会显示 http ://foo.com,而不是 http://mydomain.com/app1

)这就是 MVC 开始抱怨的地方。 Url.Content() 和Html.Action() 生成意外的 URL。 例如,在 Html.Action() 的情况下,如果我有 @Html.ActionLink("About", "Index", "About"),我期望 http://foo.com/about,但最终得到 http://foo.com/app1/about

因为 http://foo.com/app1/about 转换为 http://mydomain。 com/app1/app1/about,显然该链接不起作用。

同样,对于 Url.Content,如果我有 href="@Url.Content("~/Content/Site.css")",浏览器将无法加载样式表,因为它认为位置位于 http://foo.com/app1/Content/Site.css ,而不是 http://foo.com/Content/Site.css

有没有办法,我可以让 MVC 忘记它的起始目录(即从生成的 URL 中删除“app1/”?)

感谢您的阅读,如果此处需要其他信息,请告诉我。

(Note: tried to look for similar question, but the closest one doesn't seem to match my question still. i.e. MVC - Application root appears twice in url using Url.Content/Url.Action )

I currently have two ASP.NET MVC 3 RC apps running under one domain i.e.

mydomain.com/app1

mydomain.com/app2

Now, because it is a separate app, I signed up for another domain name and enabled mask forwarding to one of the apps
(i.e. http://foo.com would load http://mydomain.com/app1 and in the browser, it will only show http://foo.com, not http://mydomain.com/app1)

Well, that's where MVC starts to have some complaints. Both Url.Content() & Html.Action() generate unexpected URLs.
For example, in the case of Html.Action(), if I have @Html.ActionLink("About", "Index", "About"), I expect http://foo.com/about, but end up getting http://foo.com/app1/about

Because http://foo.com/app1/about translates into http://mydomain.com/app1/app1/about, obviously the link won't work.

Similarly, for Url.Content, if I have href="@Url.Content("~/Content/Site.css")", the browser will fail to load the stylesheet, because it thinks the location is at http://foo.com/app1/Content/Site.css, instead of http://foo.com/Content/Site.css

Is there a way, I can make MVC forget about its starting directory (i.e. remove "app1/" from its generated URL?)

Thanks for reading, and please let me know if additional information is needed here.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

还不是爱你 2024-10-10 02:56:55

目前,有一些解决方法。

对于 Url.Content(),我们只需从 Url.Content("~/...") 中删除“~”,以便摆脱该相对路径(即 /app1

至于 Html.ActionLink(),解决方案看起来很混乱,因为总是假设相对路径,所以这里的解决方法是临时的。有人有更好的解决方案吗?

public static MvcHtmlString ActionLinkAbsolute(
                    this HtmlHelper<dynamic> htmlHelper, string linkText, string actionName, string controllerName)
                {
                    MvcHtmlString result = LinkExtensions.ActionLink(htmlHelper, linkText, actionName, controllerName);
                    result = new MvcHtmlString(result.ToHtmlString().Replace(VirtualPathUtility.ToAbsolute("~"), String.Empty)); 
                    return result;
                }

这里的解决方法并非没有后果。为了支持 URL 屏蔽,并且忽略相对路径, http://mydomain.com/app1 不会生成预期的链接,因为它总是会删除相对路径。它并不完美,但这是我可以忍受的缺点。

Right now, there are workarounds.

For Url.Content(), we will simply remove "~" from Url.Content("~/..."), so to get rid of that relative path (i.e. /app1)

As for Html.ActionLink(), the solution seems messy, because relative path is always assumed, so the workaround here is temporary. Anyone has a better solution out there?

public static MvcHtmlString ActionLinkAbsolute(
                    this HtmlHelper<dynamic> htmlHelper, string linkText, string actionName, string controllerName)
                {
                    MvcHtmlString result = LinkExtensions.ActionLink(htmlHelper, linkText, actionName, controllerName);
                    result = new MvcHtmlString(result.ToHtmlString().Replace(VirtualPathUtility.ToAbsolute("~"), String.Empty)); 
                    return result;
                }

The workaround here is not without its consequence. In order to support URL masking, and disregard the relative path, http://mydomain.com/app1 will not generate expected link, because it will always remove the relative path. It is not perfect, but that's a downside I can live with.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文