从querystring将参数传递给webmethod

时间:2011-05-10 20:21:56

标签: asp.net vb.net web-services

人。我正在开发一个使用ASP和VB.NET 4的网站。我也使用FullCalendar jQuery插件,但我遇到了麻烦:从querystring中捕获一个参数到.asmx中的webmethod。我试图使用Request.QueryString()引用querystring属性,但它不起作用。

这是网络方法:

<%@ WebService Language="VB" Class="wbsCalendario" %>

Imports System.Web
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.Web.Script.Services
Imports System.Collections.Generic
Imports System.Configuration.ConfigurationManager

<WebService(Namespace:="http://tempuri.org/")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)>
<ScriptService> _
Public Class wbsCalendario
Inherits System.Web.Services.WebService

<WebMethod()> _
Public Function ListarEventos(ByVal starte As String, ByVal ende As String, ByVal v As String) As List(Of CalendarioEvento)
    Dim conexaoSql As New SqlConnection(ConnectionStrings("praeConnectionString").ConnectionString)
    Dim comandoSql As SqlCommand = New SqlCommand("spListarEventosCalendario", conexaoSql)
    comandoSql.CommandType = CommandType.StoredProcedure
    comandoSql.Parameters.AddWithValue("@bitPendentes", 0)
    comandoSql.Parameters.AddWithValue("@agendamentos", "188,135")
    comandoSql.Parameters.AddWithValue("@start", FromUnixTimespan(starte))
    comandoSql.Parameters.AddWithValue("@end", FromUnixTimespan(ende))
    comandoSql.Parameters.AddWithValue("@veiculo", v)
    Dim eventos As List(Of CalendarioEvento) = New List(Of CalendarioEvento)
    Try
        conexaoSql.Open()
        Dim sdrEventos As SqlDataReader = comandoSql.ExecuteReader
        While sdrEventos.Read
            Dim evento As New CalendarioEvento
            evento.title = StrConv(sdrEventos("vchNome").ToString, VbStrConv.ProperCase)
            evento.start = ToUnixTimespan(Convert.ToDateTime(sdrEventos("vchData") + " " + sdrEventos("vchHora")))
            eventos.Add(evento)
        End While
    Catch ex As Exception

    Finally
        conexaoSql.Close()
    End Try
    comandoSql.Parameters("@bitPendentes").Value = 1
    Try
        conexaoSql.Open()
        Dim sdrEventos As SqlDataReader = comandoSql.ExecuteReader
        While sdrEventos.Read
            Dim evento As New CalendarioEvento
            evento.title = StrConv(sdrEventos("vchNome").ToString, VbStrConv.ProperCase)
            evento.start = ToUnixTimespan(Convert.ToDateTime(sdrEventos("vchData") + " " + sdrEventos("vchHora")))
            evento.color = "#6AB0D8"
            eventos.Add(evento)
        End While
    Catch ex As Exception

    Finally
        conexaoSql.Close()
    End Try
    Return eventos
End Function

Private Shared Function ToUnixTimespan(ByVal d As DateTime) As Long
    Dim time As New TimeSpan()
    time = d.ToUniversalTime().Subtract(New DateTime(1970, 1, 1, 0, 0, 0))
    Return CType(Math.Truncate(time.TotalSeconds), Int64)
End Function

Private Shared Function FromUnixTimespan(ByVal s As String) As DateTime
    Dim time As DateTime = New DateTime(1970, 1, 1, 0, 0, 0)
    Return time.AddSeconds(s)
End Function

结束班

有人可以帮助我吗?

感谢。

1 个答案:

答案 0 :(得分:0)

查询字符串中的值会自动转换为web方法中定义的参数。

如果你有这个Webmethod:

<WebMethod()> Public Sub DoWork(arg1 As Integer, arg2 As String)

并通过以下网址调用:

http://domain/webservice.asmx/DoWork?arg=2&arg2=hello

这两个查询字符串参数将被转换为两个参数arg1和arg2,这些参数可以在DoWork()方法中正常引用。