Determing一个用户友好的URL的物理路径 [英] Determing the physical path of a user friendly url

查看:173
本文介绍了Determing一个用户友好的URL的物理路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直要求看看更新旧的ASP.net Web窗体应用程序ASP.net 4.5;具体实施微软的'用户友好'的路由机制(封装的NuGet Microsoft.AspNet.FriendlyUrls)。

I have been asked to look at updating an old ASP.net Web Forms application to ASP.net 4.5; specifically to implement Microsoft's 'User Friendly' routing mechanism (NuGet Package Microsoft.AspNet.FriendlyUrls).

一般情况下,升级是简单的,但我留下了一个问题。

Generally, the upgrade was straightforward, but I am left with one problem.

最初的开发者连接/关联的元数据的XML文件的许多网页。对于例如,/Products/Tables/Oak.aspx也可能有以下元文件/Products/Tables/Oak.aspx。元

The original developer attached/associated 'meta data' XML files to many of the web pages.. For example, /Products/Tables/Oak.aspx might also have the following meta file /Products/Tables/Oak.aspx.meta

在页面加载时,它看起来'的元文件并加载它。在非重写网址的环境中,这是很容易...

When the page loads, it 'looks' for the meta file and loads it. In a non-rewritten URL environment, this was easy...

string metaUrl = Page.Request.Url.AbsolutePath + ".meta";
string metaPath = Page.Server.MapPath(metaUrl);
If (System.IO.File.Exists(metaPath)) {
   LoadMetaFile(metaPath);
}

在一个友好的URL的环境,这不是那么容易的原始URL可能被重写/产品/表/橡树或者甚至通过自定义完全重写的MapPageRoute()的定义。

In a 'Friendly URL' environment, this is not so easy as the original URL might be rewritten to /Products/Tables/Oak or maybe even rewritten completely via a custom MapPageRoute() definition.

有谁知道,如果有,我可以找到一种方法/确定页面的真正的路径?

Does anyone know if there a way that I can find/determine the 'true' path of the page?

推荐答案

发表Petriq ASP.NET WebForms的解决方案:Request.GetFriendlyUrlFileVirtualPath ()返回空字符串在我的情况下完美地工作。

The solution posted by Petriq ASP.NET WebForms: Request.GetFriendlyUrlFileVirtualPath() returns empty string works perfectly in my scenario.

有关参考,在这里是Petriq的code为他的Htt prequest扩展方法:

For reference, here is Petriq's code for his HttpRequest extension method:

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;
                    // Following line modified for case-insensitive comparison
                    if (String.Compare("/" + route.Url, request.Path, true) == 0)
                    {
                        if (route.RouteHandler.GetType() == typeof(PageRouteHandler))
                        {
                            PageRouteHandler handler = (PageRouteHandler)route.RouteHandler;

                            ret = handler.VirtualPath;
                        }
                        break;
                    }
                }
            }
          }

          return ret;
       }
   }
}

这篇关于Determing一个用户友好的URL的物理路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆