HtmlHelper.RouteLink 知道虚拟目录吗?
我遇到一个问题,我在自定义 HtmlHelper 中为 MVC 应用程序生成链接,而 RouteLink 不知道 MVC 应用程序有虚拟目录。这意味着我可以在测试中很好地调试和使用该应用程序,但它会在生产中生成无效链接。有没有办法让 RouteLink() 或 ActionLink() 在生成链接时包含已部署应用程序的虚拟目录?
这是相关的代码片段:
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());
这会为开发实例生成一个类似“/Home/Tag/Production”的链接,但是当应用程序部署到像 /foo 这样的虚拟目录时,该链接仍然是“/Home/Tag/Production” ”而不是“/foo/Home/Tag/Production”。
Phil Haack 博客上的讨论类似: http://haacked .com/archive/2009/01/30/aspnetmvc-refresh.aspx#71053
是在开发时为 MVC 应用程序设置虚拟路径的唯一解决方案吗?我希望 MVC 能够了解它运行的上下文。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是指虚拟应用程序还是虚拟目录?虚拟目录适用于将物理文件夹映射到特定的“虚拟”URL 文件夹。它们有点与 ASP.NET MVC 正交。我不知道有什么方法可以“在虚拟目录中”运行 ASP.NET MVC 应用程序,因为我认为这没有任何意义。
如果您指的是虚拟应用程序,那么当 ASP.NET MVC 应用程序在虚拟应用程序中运行时,路由会考虑到这一点。
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.