我正在使用这样的路线
routes.MapRoute(
"PageBySlug",
RouteType.Regular,
"{slug}",
new {controller = "Page", action = "Display", slug = "Default"}, null
);
将请求映射到〜/ Some-Page-Slug到〜/ Page / Display / Some-Page-Slug。
添加内容时,用户可以选择链接到现有页面来创建引用,然后我将这些链接以这种格式存储在数据存储区中:“/ Some-Page-Slug”。
当我在Cassini中运行应用程序并从数据存储区中取出链接并将其附加到A标记时,它看起来像http://localhost:93229/Some-Page-Slug并且此链接有效。
但是,当在IIS中的某个网站下的虚拟目录中运行该应用程序时,附加的链接会生成此网址http://localhost/Some-Page-Slug,应该是http://localhost/virtualdir/Some-Page-Slug。
当然,这会产生404错误。
我怎样才能解决这个问题,使其在所有情况下都具有普遍的实用性?我应该以不同的方式在数据库中存储它,还是应该在运行时在我的视图中将其转换为正确的形式以及如何执行此操作?
答案 0 :(得分:1)
我已经找到了一个解决方案:
RouteValueDictionary routeValues = new RouteValueDictionary
{
{"slug", Html.Encode(Model.Url)}
};
// the commented lines work in IIS but not is ASP.NET builtin server
//string url = UrlHelper.GenerateUrl("PageBySlug", "Display", "Page", routeValues, RouteTable.Routes,
//Request.RequestContext, true);
string url = Url.Content("~/" + Html.Encode(Model.Url)); // this works in both
如果您有其他建议,欢迎他们!