none
Visual Basic web API works for numbers but not for characters

    Pertanyaan

  • I have a web API which I wrote in visual basic.  This is the code of the controller:

    Imports System.Web.Http
    Public Class DatabaseController
        Inherits ApiController
        Private product_information As Database() = New Database() _
        {New Database() With {
            .return_response = "return response"
        }}
        <System.Web.Http.HttpGet()>
        Public Function doDatabase(<FromUri> ByVal dbRequest As String) As IEnumerable(Of Database)
    
            Dim return_string As String = ""
            Dim passedData As String = dbRequest
    
            return_string = passedData
    
            Dim mqResponse As Database() = New Database() _
                {New Database() With {
                    .return_response = Str(return_string)
                }}
            'Return product_information
            Return mqResponse
    
        End Function
    
    End Class

    I also have the following model:

    Public Class Database
        Private _Return_Response As String
        Public Property return_response() As String
            Get
                Return _Return_Response
            End Get
            Set(value As String)
                _Return_Response = value
            End Set
        End Property
    End Class

    I execute the API by entering the following url in a web browser:

    http://localhost:12976/api/Database?dbRequest=1111

    When I execute the above url, the response back from the webAPI is this:

    <ArrayOfDatabase xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/WebAPI">
    <Database>
    <return_response> 1111</return_response>
    </Database>

    </ArrayOfDatabase>

    This is exactly what I expected to get back.  However, when I change the 1111 to AAAA, I get this error:

    <Error>
    <Message>An error has occurred.</Message>
    <ExceptionMessage>
    Argument 'Number' cannot be converted to a numeric value.
    </ExceptionMessage>
    <ExceptionType>System.InvalidCastException</ExceptionType>
    <StackTrace>
    at Microsoft.VisualBasic.Conversion.Str(Object Number) at WebAPI.DatabaseController.doDatabase(String dbRequest) in C:\Users\jonat\OneDrive\Documents\Visual Studio 2015\Projects\AccuWebAPI\WebAPI\WebAPI\Controllers\DatabaseController.vb:line 16 at lambda_method(Closure , Object , Object[] ) at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass6_1.<GetExecutor>b__3(Object instance, Object[] methodParameters) at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments) at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken) --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Web.Http.Controllers.ApiControllerActionInvoker.<InvokeActionAsyncCore>d__1.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Web.Http.Controllers.ActionFilterResult.<ExecuteAsync>d__5.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__15.MoveNext()
    </StackTrace>

    </Error>

    Why would this be? I thank you for any assistance.



    jonathan small

    Selasa, 04 Desember 2018 03.23

Semua Balasan

  • Hi,

    According to your description, your issue is more related about asp.net. And this forum is discussing and asking questions about the vb.net.

    Ask in the following forum.

    https://forums.asp.net/

    Thank you for your understanding.

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Selasa, 04 Desember 2018 04.21