Hello all,
I was trying to develop web api but at the point of testing this error generated
{"Message":"No HTTP resource was found that matches the request URI 'http://localhost:3376/api/PostIndividual?Surname=Testing&FirstName=Testing&OtherNames=Testing&Sex=Male&Address1=50DKD&&Address=lagos&Phone=89979797&ReferenceNumber=596969itit&Statecode=20&UTINType=IND&sevenINT=009999'.","MessageDetail":"No type was found that matches the controller named 'PostIndividual'."}
below is the detail of my code. You can also tell me what am doing wrong why my controller post, delete and update is not responding returning the above error.
I have this in my app_start
WebApiConfig
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Web API routes
config.Routes.MapHttpRoute(
name: "WithActionApi",
routeTemplate: "api/{controller}/{action}/{customerID}"
);
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Formatters.JsonFormatter.
SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
}
This is my Global asax
public class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
GlobalConfiguration.Configure(WebApiConfig.Register);
}
}
My Parameter class in Model
Individual
public string Surname { get; set; }
public string FirstName { get; set; }
public string OtherNames { get; set; }
public string Sex { get; set; }
public string Address1 { get; set; }
public string Address { get; set; }
public string Phone { get; set; }
public string ReferenceNumber { get; set; }
public string Statecode { get; set; }
public string UTINType { get; set; }
public string sevenINT { get; set; }
This is my IIndividualRepository
public interface IIndividualRepository
{
IEnumerable<Individual> GetAll();
Individual Get(string ReferenceID);
Individual Add(Individual item);
bool Remove(string customerID);
bool Update(Individual item);
}
This is IndividualRepository
public IEnumerable<Individual> GetAll()
{
List<Individual> Individuals = new List<Individual>();
//string query = string.Format(" SELECT [Surname], [FirstName], [OtherNames], [Phone], [Address], [Address1], [Phone], [Sex] FROM [Delta].[dbo].[PayerInfo_individual] where Phone like '%08034303591%'");
string query = string.Format("SELECT [Surname], [FirstName], [OtherNames], [Phone], [Address], [Address1], [Phone], [Sex] FROM [Delta].[dbo].[PayerInfo_individual]");
string constr = ConfigurationManager.ConnectionStrings["connectionstring"].ToString();
using (SqlConnection con =
new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(query, con))
{
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
Individual Individual = new Individual();
if (reader.GetValue(0) != null)
{
Individual.Surname = reader.GetValue(0).ToString();
}
else
{
Individual.Surname = string.Empty;
}
if (reader.GetValue(1) != null)
{
Individual.FirstName = reader.GetValue(1).ToString();
}
else
{
Individual.FirstName = string.Empty;
}
if (reader.GetValue(2) != null)
{
Individual.OtherNames = reader.GetValue(2).ToString();
}
else
{
Individual.OtherNames = string.Empty;
}
if (reader.GetValue(3) != null)
{
Individual.Phone = reader.GetValue(3).ToString();
}
else
{
Individual.Phone = string.Empty;
}
if (reader.GetValue(4) != null)
{
Individual.Address = reader.GetValue(4).ToString();
}
else
{
Individual.Address = string.Empty;
}
if (reader.GetValue(5) != null)
{
Individual.Address1 = reader.GetValue(5).ToString();
}
else
{
Individual.Address1 = string.Empty;
}
if (reader.GetValue(6) != null)
{
Individual.Phone = reader.GetValue(6).ToString();
}
else
{
Individual.Phone = string.Empty;
}
if (reader.GetValue(7) != null)
{
Individual.Sex = reader.GetValue(7).ToString();
}
else
{
Individual.Sex = string.Empty;
}
Individuals.Add(Individual);
}
con.Close();
}
}
return Individuals.ToArray();
}
public Individual Get(string PHONE)
{
Individual customer = new Individual();
//string query = string.Format(" SELECT [Surname], [FirstName], [OtherNames], [Phone], [Address], [Address1], [Phone], [Sex] FROM [Delta].[dbo].[PayerInfo_individual] " +
// " WHERE Phone LIKE '%"+ PHONE + "%'");
string query = string.Format(" SELECT [Surname], [FirstName], [OtherNames], [Phone], [Address], [Address1], [Phone], [Sex] FROM [Delta].[dbo].[PayerInfo_individual] where Phone like '%" + PHONE + "%'");
string constr = ConfigurationManager.ConnectionStrings["connectionstring"].ToString();
using (SqlConnection con =
new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(query, con))
{
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
Individual Individual = new Individual();
if (reader.GetValue(0) != null)
{
Individual.Surname = reader.GetValue(0).ToString();
}
else
{
Individual.Surname = string.Empty;
}
if (reader.GetValue(1) != null)
{
Individual.FirstName = reader.GetValue(1).ToString();
}
else
{
Individual.FirstName = string.Empty;
}
if (reader.GetValue(2) != null)
{
Individual.OtherNames = reader.GetValue(2).ToString();
}
else
{
Individual.OtherNames = string.Empty;
}
if (reader.GetValue(3) != null)
{
Individual.Phone = reader.GetValue(3).ToString();
}
else
{
Individual.Phone = string.Empty;
}
if (reader.GetValue(4) != null)
{
Individual.Address = reader.GetValue(4).ToString();
}
else
{
Individual.Address = string.Empty;
}
if (reader.GetValue(5) != null)
{
Individual.Address1 = reader.GetValue(5).ToString();
}
else
{
Individual.Address1 = string.Empty;
}
if (reader.GetValue(6) != null)
{
Individual.Phone = reader.GetValue(6).ToString();
}
else
{
Individual.Phone = string.Empty;
}
if (reader.GetValue(7) != null)
{
Individual.Sex = reader.GetValue(7).ToString();
}
else
{
Individual.Sex = string.Empty;
}
}
con.Close();
}
}
return customer;
}
public Individual Add(Individual item)
{
SqlConnection con;
SqlCommand com;
string constr = ConfigurationManager.ConnectionStrings["connectionstring"].ToString();
con = new SqlConnection(constr);
com = new SqlCommand("usp_MobileSavingIdividual", con);
com.CommandType = CommandType.StoredProcedure;
com.Parameters.AddWithValue("@Surname", item.Surname);
com.Parameters.AddWithValue("@FirstName", item.FirstName);
com.Parameters.AddWithValue("@OtherNames", item.OtherNames);
com.Parameters.AddWithValue("@Sex", item.Sex);
com.Parameters.AddWithValue("@Address1", item.Address1);
com.Parameters.AddWithValue("@Address", item.Address);
com.Parameters.AddWithValue("@Phone", item.Phone);
com.Parameters.AddWithValue("@ReferenceNumber", item.ReferenceNumber);
com.Parameters.AddWithValue("@Statecode", item.Statecode);
com.Parameters.AddWithValue("@UTINType", item.UTINType);
com.Parameters.AddWithValue("@sevenINT", item.sevenINT);
con.Open();
com.ExecuteNonQuery();
con.Close();
return item;
}
public bool Remove(string customerID)
{
string query = string.Format("DELETE FROM [Delta].[dbo].[PayerInfo_individual] WHERE Phone LIKE '{0}", customerID);
string constr = ConfigurationManager.ConnectionStrings["connectionstring"].ToString();
using (SqlConnection con =
new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(query, con))
{
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
return true;
}
public bool Update(Individual item)
{
string query = string.Format("UPDATE [Delta].[dbo].[PayerInfo_individual] " +
" SET [Surname] = '{0}'," +
" [Firstname] = '{1}', " +
" [OtherNames] = '{2}', " +
" [Sex] = '{3}', " +
" [Address] = '{4}', " +
" [Address1] = '{5}', " +
" [Address] = '{6}', " +
" [Phone] = '{7}' " +
" WHERE Phone LIKE '{8}'", item.Surname, item.FirstName, item.OtherNames, item.Sex, item.Address, item.Address1, item.Address,
item.Phone, item.Phone);
string constr = ConfigurationManager.ConnectionStrings["connectionstring"].ToString();
using (SqlConnection con =
new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(query, con))
{
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
return true;
}
This is my controller
public class IndividualController : ApiController
{
static readonly IIndividualRepository repository = new IndividualRepository();
public IEnumerable<Individual> GetAllIndividual()
{
return repository.GetAll();
}
public Individual GetIndividual(string ind)
{
Individual individual = repository.Get(ind);
if (individual == null)
{
throw new HttpResponseException(HttpStatusCode.NotFound);
}
return individual;
}
public HttpResponseMessage PostIndividual(Individual indivual)
{
indivual = repository.Add(indivual);
var response = Request.CreateResponse<Individual>(HttpStatusCode.Created, indivual);
string uri = Url.Link("DefaultApi", new { Phone = indivual.Phone });
response.Headers.Location = new Uri(uri);
return response;
}
public void UpdateIndividual(string ind, Individual indprofile)
{
indprofile.Phone = ind;
if (!repository.Update(indprofile))
{
throw new HttpResponseException(HttpStatusCode.NotFound);
}
}
public void DeleteIndividul(string Ind)
{
Individual customer = repository.Get(Ind);
if (customer == null)
{
throw new HttpResponseException(HttpStatusCode.NotFound);
}
repository.Remove(Ind);
}
}
This work perfectly
http://localhost:3376/api/individual/GetAllIndividual
But at post with this
localhost:3376/api/PostIndividual?Surname=Testing&FirstName=Testing&OtherNames=Testing&Sex=Male&Address1=50DKD&&Address=lagos&Phone=89979797&ReferenceNumber=596969itit&Statecode=20&UTINType=IND&sevenINT=009999
Generate This error.
{"Message":"No HTTP resource was found that matches the request URI 'http://localhost:3376/api/PostIndividual?Surname=Testing&FirstName=Testing&OtherNames=Testing&Sex=Male&Address1=50DKD&&Address=lagos&Phone=89979797&ReferenceNumber=596969itit&Statecode=20&UTINType=IND&sevenINT=009999'.","MessageDetail":"No type was found that matches the controller named 'PostIndividual'."}
and if i do this
http://localhost:3376/api/Individual/PostIndividual?Surname=Testing&FirstName=Testing&OtherNames=Testing&Sex=Male&Address1=50DKD&&Address=lagos&Phone=89979797&ReferenceNumber=596969itit&Statecode=20&UTINType=IND&sevenINT=009999
It calls GetAllIndiviual Method.
Please can you tell what am doing wrong
Thank you