1

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

例如查询网址:

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

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

全球.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
4

1 回答 1

1

如果您继承自ServiceServiceStack的新 APIServiceBase(of T)(如果您想继续使用旧 API),您将有权访问base.Requestbase.Response属性,您可以使用这些属性来获得对 HTTP API 的细粒度访问,例如:

var someParam = base.Request.QueryString["SomeParam"];

否则,如果您想让您的服务只实现IService接口,那么您还需要实现IRequiresRequestContext哪个让 ServiceStack 将请求的 RequestContext 注入您的服务。在这种情况下,您可以通过以下方式访问 QueryString:

var httpReq = base.RequestContext.Get<IHttpRequest>();

    var someParam = httpReq.QueryString["SomeParam"];

于 2012-11-19T15:46:16.377 回答