ASP.NET WebForms:Request.GetFriendlyUrlFileVirtualPath()返回空字
本文关键字:返回 GetFriendlyUrlFileVirtualPath NET WebForms Request ASP | 更新日期: 2023-09-27 18:30:00
我有一个只有几个页面的简单ASP.NET WebForms项目,我想使用友好的URL(不仅用于SEO,还用于URL的本地化)。我在这个项目中使用.NET 4.5,并尝试使用Microsoft.AspNet.FriendlyUrls nuget包,看起来可能会有所帮助。但有一个问题。
我在Global.asax上看到了这个:
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
BundleConfig.RegisterBundles(BundleTable.Bundles);
RouteConfig.RegisterRoutes(RouteTable.Routes);
}
RouteConfig类如下所示:
public static class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapPageRoute("HowItWorks", "ako-to-funguje", "~/HowItWorks.aspx");
routes.MapPageRoute("AboutUs", "o-nas", "~/AboutUs.aspx");
routes.MapPageRoute("Contact", "kontakt", "~/Contact.aspx");
var settings = new FriendlyUrlSettings();
settings.AutoRedirectMode = RedirectMode.Permanent;
routes.EnableFriendlyUrls(settings);
}
}
如果您通过/HowItWorks和/ako访问funguje(斯洛伐克语言环境),我想显示相同的页面。
现在我已经接近实际问题了。当我使用一个本地化的路由(例如/ako到funguje)访问网站时,Request.GetFriendlyUrlFileVirtualPath()返回空字符串(但我想得到"~/HowItWorks.aspx",我想在主页上做一些事情)。
string pageFileName = Request.GetFriendlyUrlFileVirtualPath();
switch (pageFileName)
{
case "~/AboutUs.aspx":
//do some stuff
break;
case "~/HowItWorks.aspx":
//do some stuff
break;
case "~/Contact.aspx":
//do some stuff
break;
default:
break;
}
如果我使用/HowItWorksURL访问网站,那么Request.GetFriendlyUrlFileVirtualPath()会按预期返回"~/HowItWorks.aspx"。
知道如何在通过/HowItWorks和/ako访问网站时从Request.GetFriendlyUrlFileVirtualPath()获得"~/HowItWorks.aspx"吗?
最后,我提出了自己的解决方法。我为http请求创建了自己的扩展方法,该方法返回当前执行页面的虚拟文件路径:
using System.Web;
using System.Web.Routing;
using Microsoft.AspNet.FriendlyUrls;
namespace Utils.Extensions
{
public static class HttpRequestExtensions
{
public static string GetFileVirtualPathFromFriendlyUrl(this HttpRequest request)
{
string ret = string.Empty;
ret = request.GetFriendlyUrlFileVirtualPath();
if (ret == string.Empty)
{
foreach (RouteBase r in RouteTable.Routes)
{
if (r.GetType() == typeof(Route))
{
Route route = (Route)r;
if ("/" + route.Url == request.Path)
{
if (route.RouteHandler.GetType() == typeof(PageRouteHandler))
{
PageRouteHandler handler = (PageRouteHandler)route.RouteHandler;
ret = handler.VirtualPath;
}
break;
}
}
}
}
return ret;
}
}
}
请与核实
string requestUrl = this.Request.GetFriendlyUrlFileVirtualPath().ToLowerInvariant();
if (String.IsNullOrEmpty(requestUrl)) {
requestUrl = this.Request.AppRelativeCurrentExecutionFilePath.ToLowerInvariant();
}
我更新了Petriq的答案以处理url 中的参数
public static string GetFileVirtualPathFromFriendlyUrl(this HttpRequest request) {
string ret = request.GetFriendlyUrlFileVirtualPath();
if (ret == string.Empty) {
for(int j = 0, a = RouteTable.Routes.Count; j<a;j++) {
RouteBase r = RouteTable.Routes[j];
if (r.GetType() == typeof(Route)) {
Route route = (Route)r;
StringBuilder newroute = new StringBuilder(route.Url);
if (route.Defaults != null && route.Defaults.Count > 1) {
string[] keys = route.Defaults.Select(x => x.Key).ToArray();
foreach (string k in keys) {
newroute = newroute.Replace("{" + k + "}", k.CheckQueryString());
}
}
if (String.Compare(newroute.ToString(), request.Path.Replace(request.ApplicationPath, ""), true) == 0) {
if (route.RouteHandler.GetType() == typeof(PageRouteHandler)) {
PageRouteHandler handler = (PageRouteHandler)route.RouteHandler;
return handler.VirtualPath;
}
}
}
}
}
return ret;
}
路线现在可以是
RouteValueDictionary contactDefault = new RouteValueDictionary { { "person", UrlParameter.Optional } };
routes.MapPageRoute("Contact", "kontakt/{person}", "~/Contact.aspx");