问题描述
我有只有几页简单的ASP.NET WebForms的项目,我想使用友好的URL(不仅是搜索引擎优化,也网址本地化)。我使用.NET 4.5的这个项目,并试图利用Microsoft.AspNet.FriendlyUrls的NuGet包,它看起来像它可能会帮助。但有一个问题。
I have got simple ASP.NET WebForms project with only few pages and I would like to use friendly URLs (not only for SEO but also localization of URLs). I use .NET 4.5 for this project and have tried to use Microsoft.AspNet.FriendlyUrls nuget package which looked like it might help. But there is one issue.
我有在Global.asax中这样的:
I've got in Global.asax this:
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
BundleConfig.RegisterBundles(BundleTable.Bundles);
RouteConfig.RegisterRoutes(RouteTable.Routes);
}
和RouteConfig类看起来是这样的:
and RouteConfig class looks like this:
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(这是斯洛伐克环境)。
I want to show the same page if you access it through both /HowItWorks and /ako-to-funguje (which is Slovak locale).
,现在我越来越接近实际问题。当我访问的网站提供本地化的路线(如/ AKO到funguje),然后Request.GetFriendlyUrlFileVirtualPath()返回空字符串之一(但我想获得〜/ HowItWorks.aspx赖以我想要做的主人一些东西页)。
And now I am getting close to actual issue. When I access site with one of localized routes (e.g. /ako-to-funguje) then Request.GetFriendlyUrlFileVirtualPath() returns empty string (but I want to get "~/HowItWorks.aspx" upon which I want to do some stuff in master page).
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;
}
如果我访问站点/ HowItWorks URL然后Request.GetFriendlyUrlFileVirtualPath()返回〜/ HowItWorks.aspx作为预期。
If I access site with /HowItWorks URL then Request.GetFriendlyUrlFileVirtualPath() returns "~/HowItWorks.aspx" as expected.
不知道如何得到〜/ HowItWorks.aspx从Request.GetFriendlyUrlFileVirtualPath()通过两个/ HowItWorks和/ AKO到funguje访问网站的时候?
Any idea how to get "~/HowItWorks.aspx" from Request.GetFriendlyUrlFileVirtualPath() when accessing site through both /HowItWorks and /ako-to-funguje?
推荐答案
最后我想出了这个问题的解决方法本身。我已经为HTTP请求自己的扩展方法,当前执行的页面,返回虚拟文件路径:
At the end I've come up with own workaround of this issue. I've created own extension method for http request which returns virtual file path to currently executed page:
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;
}
}
}
这篇关于ASP.NET的WebForms:Request.GetFriendlyUrlFileVirtualPath()返回空字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!