开发者

URL forwarding+masking triggers double root directory for Url.Content() & Html.Action() under ASP.NET MVC 3 RC

(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 fai开发者_StackOverflow中文版l 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.


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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜