【问题标题】:Different variable name from querystring与查询字符串不同的变量名
【发布时间】:2013-06-18 03:37:20
【问题描述】:

我的控制器有一个对象作为参数

Function Search(ByVal model As ItemSearchModel) As ActionResult

看起来像这样

Public Class ItemSearchModel

    Public Property SearchQuery As String

而且,您可以想象,网址看起来像这样

/Search?SearchQuery=test

我想把查询字符串改成一个小变量,有点像

/Search?s=test

有没有一种内置方法可以在我的班级中保留相同的变量名?类似的东西

Public Class ItemSearchModel

    <QueryString(Name:="s")> _
    Public Property SearchQuery As String

【问题讨论】:

    标签: asp.net-mvc


    【解决方案1】:

    我认为你可以使用 Nuget 的 ActionParameterAlias 包来完成你想要的。

    【讨论】:

      【解决方案2】:

      您可以定义两个属性,都指向同一个字段。然后,您可以使用 URL 中的s 或SearchQuery 访问该项目。

      Public Class ItemSearchModel
          Private _s As String
      
          Public Property s() As String
              Get
                  Return _s
              End Get
              Set(value As String)
                  _s = value
              End Set
          End Property
          Public Property SearchQuery() As String
              Get
                  Return _s
              End Get
              Set(value As String)
                  _s = value
              End Set
          End Property
      End Class
      

      【讨论】:

        猜你喜欢
        • 2017-04-13
        • 2014-06-20
        • 2011-07-23
        • 1970-01-01
        • 2011-06-09
        • 2019-02-26
        • 2014-04-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多