System.FormatException:String应该仅包含十六进制数字... MongoDB,WebApi

时间:2019-04-30 07:59:45

标签: json vb.net mongodb asp.net-web-api crud

它是MongoDB中的CRUD程序,并在另一个项目中使用Web API。在执行GET操作以显示集合中的所有值的同时,该调用直接执行,我们通过id获取数据。

当我检查邮递员中的URI时,它显示错误

  

System.FormatException:字符串应仅包含十六进制数字

我不知道为什么在任何上下文中都没有调用id参数时,为什么会传递id并转到该函数。

Contact.cs

Public Class Contact

    <BsonId>
    <BsonRepresentation(BsonType.ObjectId)>
    Public Property Id As String
    Public Property Name As String
    Public Property Address As String
    Public Property Phone As String
    Public Property Email As String
End Class

ContactOperation.vb(CRUD操作)

Public Class ContactOperation

    Protected Shared _client As IMongoClient
    Protected Shared _database As IMongoDatabase
    Protected Shared _collection As IMongoCollection(Of Contact)

    Public Sub New()
        _client = New MongoClient("mongodb://localhost:27017")
        _database = _client.GetDatabase("mydb")
        _collection = _database.GetCollection(Of Contact)("contact")
    End Sub

    Public Async Function GetAllContact() As Task(Of IEnumerable(Of Contact))
        Dim filter = New BsonDocument()
        Dim result = Await _collection.Find(filter).ToListAsync()
        Dim test As String = "Test"
        Return result
    End Function

    Public Async Function GetContact(ByVal id As Object) As Task(Of Contact)
        Dim filter = Builders(Of Contact).Filter.Eq(Function(e) e.Id, id)
        Dim result = Await _collection.Find(filter).FirstOrDefaultAsync()
        Return result
    End Function
End Class

ValuesController

Public Class ValuesController
    Inherits ApiController

    Private _contactOp As ContactOperation
    Public Sub New()
        _contactOp = New ContactOperation()
    End Sub

    <HttpGet>
    Public Async Function [Get]() As Task(Of IHttpActionResult)
        Dim contact = Await _contactOp.GetAllContact()
        Return Ok(contact)
    End Function

    <HttpGet>
    Public Async Function [Get](ByVal id As String) As Task(Of IHttpActionResult)
        Dim contact = Await _contactOp.GetContact(New ObjectId(id))
        Return Ok(contact)
    End Function
End Class

ValuesController (使用Web API)不同的项目:

 Public Class ValuesController
        Inherits Controller

        Function Index() As ActionResult
            Dim contacts As IEnumerable(Of Contact) = Nothing
            Using client = New HttpClient()
                client.BaseAddress = New Uri("http://localhost:57169/api/values/get")
                Dim responseTask = client.GetAsync("contact")
                responseTask.Wait()
                Dim result = responseTask.Result

                If result.IsSuccessStatusCode Then
                    Dim readTask = result.Content.ReadAsAsync(Of IList(Of Contact))()
                    readTask.Wait()
                    contacts = readTask.Result
                Else
                    contacts = Enumerable.Empty(Of Contact)()
                    ModelState.AddModelError(String.Empty, "Server error. Please contact administrator.")
                End If
            End Using
            Return View(contacts)
        End Function
End Class

在POSTMAN中测试api时的错误屏幕截图,如下所示 POSTMAN

下面给出了进行其他操作的呼叫的屏幕截图 Call going wrong

我不知道为什么id会像调用没有值的get方法一样将值作为参数来获取!我是mongodb和Web API的初学者!请帮忙!

0 个答案:

没有答案