I have an issue where I’m generating links for my MVC app in a custom HtmlHelper, and RouteLink isn’t aware that the MVC application has a virtual directory. This means I can debug and use the app fine in testing, but it generates invalid links in production. Is there a way to get RouteLink() or ActionLink() to include the virtual directory for the deployed application when links get generated?
Here’s the relevant code snippet:
private const string STagLink = "<span class=\"tag-{0}\">{1}</span>\r\n";
...
MvcHtmlString link = html.RouteLink(x.Name,
new { Tag = x.Name, Controller = x.Controller, Action = x.Action },
new { @class = "tag", Title = x.Title });
sb.AppendFormat(STagLink, j, link.toHtmlString());
This generates a link like: “/Home/Tag/Production” for the development instance, but when the application is deployed to a virtual directory like /foo, the link is still “/Home/Tag/Production” instead of “/foo/Home/Tag/Production”.
This discussion on Phil Haack’s blog is similar: http://haacked.com/archive/2009/01/30/aspnetmvc-refresh.aspx#71053
Is the only solution to set an virtual path for the MVC application at development time? I was hoping MVC would be aware of the context in which it is running.
Do you mean Virtual Application or Virtual Directory? Virtual Directories apply to mapping physical folders to specific “virtual” URL folders. They are sort of orthogonal to ASP.NET MVC. I don’t know of any way to run an ASP.NET MVC application “within a virtual directory” as I don’t think that makes any sense.
If you’re referring to Virtual Applications, then routing does take that into account when an ASP.NET MVC application is running within a virtual application.