I am trying to register using mvc web api in a simple way:
This is my users model:
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Namespace WebApi.Models
Public Class users
Public Property userid() As Guid
Public Property logintype() As String
Public Property username() As String
Public Property password() As String
Public Property email() As String
Public Property createddate() As DateTime
End Class
End Namespace
This is my userdetails model:
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Data
Imports System.Data.SqlClient
Namespace WebApi.Models
Public Class userdetails
Private users As New List(Of users)()
Private con As SqlConnection
Private da As SqlDataAdapter
Private ds As New DataSet()
Dim userid As String = System.Guid.NewGuid.ToString()
Public Function RegisterUser(ByVal username As String, ByVal password As String, ByVal email As String, ByVal logintype As String) As String
'Create ConnectionString and Inser Statement
Try
Using cn As New SqlConnection("Data Source=.\sqlexpress;Initial Catalog=usersList;Persist Security Info=True;User ID=sa;Password=*****")
cn.Open()
Dim cmd As New SqlCommand()
cmd.CommandText = "INSERT INTO users (userid,logintype,username, password, email, createddate) VALUES(@userid,@logintype,@username,@password,@email,@createddate)"
Dim strDate As String = Date.Now.ToString("MM/dd/yyyy hh:mm:ss tt")
Dim param1 As New SqlParameter()
param1.ParameterName = "@userid"
param1.Value = userid
cmd.Parameters.Add(param1)
Dim param2 As New SqlParameter()
param2.ParameterName = "@logintype"
param2.Value = logintype
cmd.Parameters.Add(param2)
Dim param3 As New SqlParameter()
param3.ParameterName = "@username"
param3.Value = username
cmd.Parameters.Add(param3)
Dim param4 As New SqlParameter()
param4.ParameterName = "@password"
param4.Value = password
cmd.Parameters.Add(param4)
Dim param5 As New SqlParameter()
param5.ParameterName = "@email"
param5.Value = email
cmd.Parameters.Add(param5)
Dim param6 As New SqlParameter()
param6.ParameterName = "@createddate"
param6.Value = strDate
cmd.Parameters.Add(param6)
cmd.Connection = cn
cmd.ExecuteNonQuery()
cn.Close()
End Using
Return True
Catch
Return False
End Try
End Function
Public Function GetAll() As IEnumerable(Of users)
con = New SqlConnection("Data Source=.\sqlexpress;Initial Catalog=usersList;Persist Security Info=True;User ID=sa;Password=*****")
da = New SqlDataAdapter("select * from users", con)
da.Fill(ds)
For Each dr As DataRow In ds.Tables(0).Rows
users.Add(New users() With {.userid = Guid.Parse(dr(0).ToString()), .logintype = dr(1).ToString(), .username = dr(2).ToString(), .password = dr(3).ToString(), .email = dr(4).ToString(), .createddate = DateTime.Parse(dr(5).ToString())})
Next dr
Return users
End Function
Public Function GetUserById(ByVal userid As String) As String
Dim con As New SqlConnection("Data Source=.\sqlexpress;Initial Catalog=usersList;Persist Security Info=True;User ID=sa;Password=*****")
con.Open()
Dim email As String = String.Empty
Dim cmd As New SqlCommand("select * from users where userid=@userid", con)
cmd.Parameters.AddWithValue("@userid", userid)
Using sdr As SqlDataReader = cmd.ExecuteReader()
If sdr.Read() Then
email = sdr("email").ToString()
End If
End Using
con.Close()
Return email
End Function
End Class
End Namespace
This is my userdetails controller:
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Net
Imports System.Net.Http
Imports System.Web.Http
Imports MvcApplication1.WebApi.Models
Namespace WebApi.Controllers
Public Class usercontroller
Inherits ApiController
Private Shared ReadOnly repository As New userdetails()
Public Function RegisterUser(<FromBody()> ByVal username As String, <FromBody()> ByVal password As String, <FromBody()> ByVal email As String, <FromBody()> ByVal logintype As String) As String
Return repository.RegisterUser(username, password, email, logintype)
End Function
Public Function GetAllUsers() As IEnumerable(Of users)
Return repository.GetAll()
End Function
Public Function GetEmail(ByVal id As String) As String
Return repository.GetUserById(id)
End Function
End Class
End Namespace
Now I'm able to get the userdetails with Id but when trying to post the details its not hitting the breakpoint and this is how I'm trying to post using fiddler and getting 500 response:
Can anyone say me how do I post the data in a correct way?
Aucun commentaire:
Enregistrer un commentaire