【问题标题】:ASP.Net Context.RewritePath does not work when original URL contains query string当原始 URL 包含查询字符串时,ASP.Net Context.RewritePath 不起作用
【发布时间】:2012-12-13 18:37:31
【问题描述】:

我正在编写 IHttpModule 以允许对我们的详细信息页面之一进行友好的 URL 访问。当用户尝试使用http://xyx.com/hotels/123/hotel-name.aspx 页面访问详细信息页面时,将在 IHttpModule 的 OnBeginRequest 方法中应用重写规则。

在这里,我写了使用 Context.RewritePath("~/DetailPages/DetailPage.aspx", "", "Code=123") 将路径更改为“~/DetailPages/DetailPage.aspx”,其中 123 是采用的代码来自原始网址。

现在,如果原始 URL 不包含任何查询字符串,则会调用详细信息页面。但是当用户尝试访问http://xyz.com/hotels/123/hotel-name.aspx?show=advance 时,重写代码将作为Context.RewritePath("~/DetailPages/DetailPage.aspx", "", "Code=123&show=advance") 执行。在这里,我们收到一条错误消息,提示“/hotels/123/hotel-name.aspx”页面不存在。

在调用“Context.RewritePath”方法之前和之后,我从下面给出的Context.Request 对象中获取了详细信息。如果原始 URL 中没有查询字符串,两者看起来都很完美且相同。但是当原始 URL 包含查询字符串时,在“Context_Error”(侦听此请求执行的任何错误的事件)方法中,当获取 Server.GetLastError() 时说“/hotels/123/hotel-name.aspx”页面不存在。

请参阅下面来自Context.Request 对象的跟踪详细信息。欢迎您的帮助/意见,因为我已经花了 8 小时没有任何积极的结果。

-------------- 之前 - RewritePath 调用 ---------------------------------------

RawUrl : - /Hotels/123/hotel-name.aspx?a=b

AppRelativeCurrentExecutionFilePath : - ~/Hotels/123/hotel-name.aspx

CurrentExecutionFilePath : - /Hotels/123/hotel-name.aspx

参数:-a=b&__utma=221736730.1213284380.1356606190.1356674469.1356687004.4.....(Windows+NT+5.1%3b+rv%3a7.0.1)+Gecko%2f20100101+Firefox%2f7.0.1

路径:- /Hotels/123/hotel-name.aspx

PhysicalPath : - D:\AppRoot\Hotels\161\hotel-name.aspx

网址:-http://xyz.com/Hotels/123/hotel-name.aspx?a=b

-------------- 之后 - RewritePath 调用 ---------------------------------------

RawUrl : - /Hotels/123/hotel-name.aspx?a=b

AppRelativeCurrentExecutionFilePath : - ~/DetailPages/DetailPage.aspx

CurrentExecutionFilePath : - /DetailPages/DetailPage.aspx

参数:-代码=123&a=b&__utma=221736730.1213284380.1356606190.1356674469.1356687004.4.....(Windows+NT+5.1%3b+rv%3a7.0.1)+Gecko%2f20100101+Firefox%2f7.0.1 >

路径:- /DetailPages/DetailPage.aspx

PhysicalPath : - D:\AppRoot\DetailPages\DetailPage.aspx

网址:-http://xyz.com/DetailPages/DetailPage.aspx?Code=123&a=b

---Server.GetLastError - Trace - Inside IHttpModule 的错误事件--

RawUrl : - /Hotels/123/hotel-name.aspx?a=b

AppRelativeCurrentExecutionFilePath : - ~/Hotels/123/hotel-name.aspx

CurrentExecutionFilePath : - /Hotels/123/hotel-name.aspx

参数:-a=b&__utma=221736730.1213284380.1356606190.1356674469.1356687004.4....(Windows+NT+5.1%3b+rv%3a7.0.1)+Gecko%2f20100101+Firefox%2f7.0.1

路径:- /Hotels/123/hotel-name.aspx

PhysicalPath : - D:\AppRoot\Hotels\161\hotel-name.aspx

网址:-http://xyz.com/Hotels/123/hotel-name.aspx?a=b

错误信息 文件“/Hotels/123/hotel-name.aspx”不存在。

堆栈跟踪

at System.Web.UI.Util.CheckVirtualFileExists(VirtualPath virtualPath)
       at System.Web.Compilation.BuildManager.GetVPathBuildResultInternal(VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile)
       at System.Web.Compilation.BuildManager.GetVPathBuildResultWithNoAssert(HttpContext context, VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile)
       at System.Web.Compilation.BuildManager.GetVirtualPathObjectFactory(VirtualPath virtualPath, HttpContext context, Boolean allowCrossApp, Boolean noAssert)
       at System.Web.Compilation.BuildManager.CreateInstanceFromVirtualPath(VirtualPath virtualPath, Type requiredBaseType, HttpContext context, Boolean allowCrossApp, Boolean noAssert)
       at System.Web.UI.PageHandlerFactory.GetHandlerHelper(HttpContext context, String requestType, VirtualPath virtualPath, String physicalPath)
       at System.Web.UI.PageHandlerFactory.System.Web.IHttpHandlerFactory2.GetHandler(HttpContext context, String requestType, VirtualPath virtualPath, String physicalPath)
       at System.Web.HttpApplication.MapHttpHandler(HttpContext context, String requestType, VirtualPath path, String pathTranslated, Boolean useAppConfig)
       at System.Web.HttpApplication.MapHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
       at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

谁能找出可能是什么问题?

提前致谢。

【问题讨论】:

  • 为什么不用微软提供的重写模块呢? iis.net/learn/extensions/url-rewrite-module/…
  • 是在 IIS 上吗?如果有,是什么版本?
  • 这是在 IIS 5.5 (Win XP) 上并将部署在 IIS7 上。我在这里担心的是,为什么当原始 URL 包含查询字符串时它不起作用但在没有查询字符串时起作用。如果我解决了这个问题,我就完成了我所需要的。但我会检查内置的 IIS 重写模块。感谢您的建议。

标签: asp.net rewritepath


【解决方案1】:

在原始 URL 中找到查询字符串时,有另一个 IHttpModule 更改 URL。因此我得到了我需要的东西。

感谢大家的善意建议和关注。

【讨论】:

    猜你喜欢
    • 2011-05-14
    • 1970-01-01
    • 1970-01-01
    • 2016-09-25
    • 2013-06-05
    • 1970-01-01
    • 2014-01-02
    • 2012-07-25
    • 2015-09-23
    相关资源
    最近更新 更多