I'm unable to Post a string to a WebAPI from a Console Application:
Console App:
public void Main()
{
try
{
ProcessData().Wait();
}
catch (Exception e)
{
logger.Log(LogLevel.Info, e);
}
}
private static async Task ProcessData()
{
try
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri(API_BASE_URL);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
// HTTP POST
var response = await client.PostAsJsonAsync("api/teste", "test_api");
}
}
catch (Exception e)
{
logger.Log(LogLevel.Info, e);
}
I try to call a WebAPI in an MVC4 Web Application:
namespace Heelp.Api
{
public class TesteController : ApiController
{
private readonly ICommentService _commentService;
public TesteController(ICommentService commentService)
{
_commentService = commentService;
}
public string Post(string comment)
{
var response = "OK";
try
{
_commentService.Create(new Core.DtoModels.CommentDto { CommentType = "Like", Email = "p@p.pt", Message = "comment" });
}
catch(Exception e)
{
response = e.Message;
}
return response;
}
}
}
[EDIT]
After testing with fiddler, I get the error:
{"Message":"An error has occurred.","ExceptionMessage":"Type 'Heelp.Api.TesteController' does not have a default onstructor",
"ExceptionType":"System.ArgumentException","StackTrace":"
at System.Linq.Expressions.Expression.New(Type type)\r\n at
System.Web.Http.Dispatcher.DefaultHttpControllerActivator.GetInstanceOrActivator(HttpRequestMessage request, Type controllerType, Func`1& activator)\r\n at
System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request,
HttpControllerDescriptor controllerDescriptor, Type controllerType)"}
The Route is the Default.
I don't know how to debug it, and why it's not working.
What am I doing wrong here?