代码之家  ›  专栏  ›  技术社区  ›  STW

ASP.NET WebForms—处理站点上所有页面的特殊查询字符串参数—URL重写、URL路由或其他方法?

  •  1
  • STW  · 技术社区  · 15 年前

    在电子商务网站上工作,该网站将与第三方供应商集成--该供应商对门店使用的标识符与我们内部使用的不同(即他们的门店ABC123是我们的001-321)。

    我正在研究检查传入请求的保留查询字符串参数的最佳方法,这些参数指示请求正在使用 他们的

    要做这个映射,我需要检查提供的ID,对数据库或缓存执行一个查找,并将请求转发到指定的页面——将修改仅限于查询字符串参数(需要维护其他参数,如HTTPHeader的详细信息等)。

    到目前为止,我正在研究几种不同的方法:

    • 在一个基本页中实现它(这已经做了太多了,但是有我们的日志基础设施和一些其他注入的依赖项的好处)
    • 在IHttpModule中实现它
    • 使用URL重写
    • (看起来路由不是我想要的,如果您认为它仍然合适,请随时提供见解)

    需要——大概1%。

    然而 对于另一个集成站点,我们将在几乎每个请求上执行此映射—与上一个不同的方法是否更适合此场景?

    3 回复  |  直到 15 年前
        1
  •  1
  •   Wyatt Barnett    15 年前

    这是一个典型的例子,其中HTTP模块最有意义——您希望深入研究所有请求的URL处理。性能开销方面,您不应该有太多的问题,假定您可以正确地短路,避免在不需要的地方进行DB/缓存查找。

    在配置方面,您应该已经解决了部署和管理配置的问题,因此我怀疑是否有其他自定义模块会增加很多开销。

        2
  •  0
  •   Doug    15 年前

    希望这有帮助。。。

        3
  •  0
  •   user376516    15 年前

    Imports System.Web
    

    公共类SiteContext

    Private _viewId As Int32 
    Private _tab As String
    Private _action As String
    
    Private Sub New()
        _viewId = -1
        _tab = String.Empty
        _action = String.Empty
    
        FillContext()
    End Sub
    
    Public Shared Function Instance() As SiteContext
    ' gets the site specific context for the current request
    
        If HttpContext.Current.Items("RequestContext") Is Nothing Then
            HttpContext.Current.Items("RequestContext") = New SiteContext
        End If
        Return HttpContext.Current.Items("RequestContext")
    
    End Function
    
    ' fill the request context with site specific items
    Private Sub FillContext()
    
        ' iterate through all items passes via the querystring and save values to matching key property names
        For i As Int16 = 0 To _context.Request.QueryString.Count - 1
            Dim qryItem As String = _context.Request.QueryString.Keys.Item(i)
    
            Select Case qryItem
                Case "v", "view", "viewid", "vid" ' VIEW ID
                    If IsNumeric(_context.Request.QueryString(qryItem)) AndAlso CType(_context.Request.QueryString(qryItem), Double) < 10000 Then
                        _viewId = CType(_context.Request.QueryString(qryItem), Int32)
                    End If
    
                Case "tab" ' TAB ID; secondary parameter to choose sub view
                    _tab = _context.Request.QueryString(qryItem)
    
                Case "action" ' ACTION ID; tertiary parameter to choose sub-sub view
                    _action = _context.Request.QueryString(qryItem)
    
                Case Else
    
            End Select
        Next
    
    End Sub
    
    Public Property ViewId() As Int32
        Get
            Return _viewId
        End Get
        Set(ByVal Value As Int32)
            If Value < 1 Then
                Value = 1
            End If
            _viewId = Value
        End Set
    End Property
    
    Public Property Tab() As String
        Get
            Return _tab
        End Get
        Set(ByVal Value As String)
            _tab = Value.Trim
        End Set
    End Property
    
    Public Property Action() As String
        Get
            Return _action
        End Get
        Set(ByVal Value As String)
            _action = Value.Trim
        End Set
    End Property
    

    结束类

    推荐文章