在ServiceStack VB.net中提取URL参数

时间:2012-11-19 11:04:39

标签: vb.net web-services jquery servicestack

如何在ServiceStack VB.net中提取URL参数?

例如查询网址:

http://localhost:3318/api/ActionName/?callback=cb&format=json&SomeParam1=19-11-2012&SomeParam2=123123

仅供参考我正在使用jQuery AJAX发出请求。

Global.aspx

Public Class Global_asax
    Inherits System.Web.HttpApplication

    Public Class HelloAppHost
        Inherits AppHostBase

        Public Sub New()
            MyBase.New("Web Services", GetType(BiffaNetWebService).Assembly)
        End Sub

        Public Overrides Sub Configure(ByVal container As Container)
            Routes.Add(Of APIRequest)("/api").Add(Of APIRequest)("/api/{ActionName}")
        End Sub
    End Class

    Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
        Dim apphost = New HelloAppHost()
        apphost.Init()
    End Sub

End Class

WS.vb

Public Class WebService
    Implements IService(Of APIRequest)

    Public Function Execute(ByVal request As APIRequest) As Object Implements ServiceStack.ServiceHost.IService(Of APIRequest).Execute

        Dim ActionName = request.ActionName
        Dim UserData = request.UserData

        // Do some stuff here and return JSON

    End Function

End Class

1 个答案:

答案 0 :(得分:1)

如果您继承ServiceStack's New APIService中的ServiceBase(of T)(如果您想继续使用旧API),则可以访问base.Request和{{您可以使用1}}属性来获得对HTTP API的细粒度访问,例如:

base.Response

否则,如果您希望保持您的服务仅仅支持var someParam = base.Request.QueryString["SomeParam"]; 接口,那么您还需要实现IService,它会使ServiceStack将请求的RequestContext注入您的服务。在这种情况下,您可以使用以下命令访问QueryString:

IRequiresRequestContext

var someParam = httpReq.QueryString [“SomeParam”];