WebAPI giving GET method error when called in browser [Resolved]

Posted by Shashikant under C# on 7/15/2016 | Points: 10 | Views : 2924 | Status : [Member] | Replies : 3
hi!

I was currently following below link to learn WebAPI
Link: http://www.c-sharpcorner.com/UploadFile/97fc7a/webapi-restful-operations-in-webapi-using-ado-net-objects-a/

I am getting error while calling WebApi Directly in browser as:
http://localhost/WebApiDemo/api/Employees/AddEmployee


Error message:
<Error><Message>The requested resource does not support http method 'GET'.</Message></Error> 

Then I tried consuming it in following way:

POST method is :

[HttpPost]
public void AddEmployee(Employee employee)
{
//int maxId = listEmp.Max(e => e.ID);
//employee.ID = maxId + 1;
//listEmp.Add(employee);


SqlConnection myConnection = new SqlConnection();
myConnection.ConnectionString = @"Server=192.168.200.26;Database=DBCompany;User ID=abc;Password=1234;";
//SqlCommand sqlCmd = new SqlCommand("INSERT INTO tblEmployee (EmployeeId,Name,ManagerId) Values (@EmployeeId,@Name,@ManagerId)", myConnection);
SqlCommand sqlCmd = new SqlCommand();
sqlCmd.CommandType = CommandType.Text;
sqlCmd.CommandText = "INSERT INTO tblEmployee (EmployeeId,Name,ManagerId) Values (@EmployeeId,@Name,@ManagerId)";
sqlCmd.Connection = myConnection;


sqlCmd.Parameters.AddWithValue("@EmployeeId", employee.EmployeeId);
sqlCmd.Parameters.AddWithValue("@Name", employee.Name);
sqlCmd.Parameters.AddWithValue("@ManagerId", employee.ManagerId);
myConnection.Open();
int rowInserted = sqlCmd.ExecuteNonQuery();
myConnection.Close();
}



To consume :

I have created a class named Employee

Class Employee.cs

using System;
using System.Collections.Generic;
using System.Text;

namespace TestWebAPIConsume
{
class Employee
{
public int EmployeeId { get; set; }
public string Name { get; set; }
public int ManagerId { get; set; }
}
}


On Default.aspx Page:


protected void Page_Load(object sender, EventArgs e)
{

HttpClient client = new HttpClient();
string serviceUrl = "http://localhost/WebApiDemo/api/Employees/AddEmployee";
var Employee = new Employee()
{
EmployeeId = 44,
Name = "AmitKumar",
ManagerId = 21
};
try
{
HttpResponseMessage response = client.PostAsJsonAsync(serviceUrl, Employee).Result;
if (response.IsSuccessStatusCode)
{
Console.WriteLine("EmployeeAdded");
}
else
{
Console.WriteLine("{0}({1})", (int)response.StatusCode, response.ReasonPhrase);
}
}

catch (Exception ex) {
lblmsg.Text= ex.Message.ToString();
}
}


Error Details:
{StatusCode: 500, ReasonPhrase: 'Internal Server Error', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
Pragma: no-cache
Cache-Control: no-cache
Date: Fri, 15 Jul 2016 06:55:07 GMT
Server: Microsoft-IIS/8.0
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Content-Length: 172
Content-Type: application/json; charset=utf-8
Expires: -1
}}


Please help!




Responses

Posted by: Rajnilari2015 on: 7/16/2016 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 50

Up
0
Down

Resolved
I am getting error while calling WebApi Directly in browser as:
http://localhost/WebApiDemo/api/Employees/AddEmployee
Error message:
<Error><Message>The requested resource does not support http method 'GET'.</Message></Error>


Try using Postman ( https://chrome.google.com/webstore/detail/postman/fhbjgbiflinjbdggehcddcbncdddomop?hl=en )



For the second case i.e. On Default.aspx Page:
You need to accept header for JSON format as under

HttpClient client = new HttpClient();
string serviceUrl = "http://localhost/WebApiDemo/api/Employees/AddEmployee";
client.BaseAddress = new Uri(serviceUrl);

// Add an Accept header for JSON format.
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));


Hope that helps.

--
Thanks & Regards,
RNA Team

Shashikant, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Shashikant on: 7/16/2016 [Member] Starter | Points: 25

Up
0
Down
Now getting Error as:
{StatusCode: 404, ReasonPhrase: 'Not Found', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
Pragma: no-cache
Cache-Control: no-cache
Date: Sat, 16 Jul 2016 11:48:19 GMT
Server: Microsoft-IIS/8.0
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Content-Length: 220
Content-Type: application/json; charset=utf-8
Expires: -1
}}


Here is my Code in WebForm1.aspx.cs:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using APIConsume.App_Code;
using System.Net.Http;
using System.Net.Http.Headers;
namespace APIConsume
{

public partial class WebForm1 : System.Web.UI.Page
{

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack) {

HttpClient client = new HttpClient();
string serviceUrl = "http://localhost/WebApiDemo/api/Employees/AddEmployee";

client.BaseAddress = new Uri(serviceUrl);

// Add an Accept header for JSON format.
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

var Employee = new Employee()
{
EmployeeId = 44,
Name = "AmitKumar",
ManagerId = 21
};
try
{
HttpResponseMessage response = client.PostAsJsonAsync(serviceUrl, Employee).Result;
if (response.IsSuccessStatusCode)
{
Console.WriteLine("EmployeeAdded");
}
else
{
Console.WriteLine("{0}({1})", (int)response.StatusCode, response.ReasonPhrase);
}
}

catch (Exception ex)
{
//lblmsg.Text = ex.Message.ToString();
}


}
}
}
}


Thanks & Regards,
Shashikant

Shashikant, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rajnilari2015 on: 7/17/2016 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 25

Up
0
Down
@Shashikant, Error Code: 404 means Resource not found. That means the API is not available.Please check that.
Thanks.

--
Thanks & Regards,
RNA Team

Shashikant, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response