سلام.
من تازه کار با WebApi رو شروع کردم.
دارم یک مقاله رو می خونم و طبق اون کار رو یاد میگیرم.
من در ابتدا از یک کنترلر استفاده کردم بصورت زیر

public class EmployeController : ApiController
{
public string[] Get()
{
return new string[]
{
"Hello",
"Nabi"
};


}
}


و جواب مناسب رو گرفتم.
اما وقتی میام و یک پوشه service می سازم و و کد ها رو وارد می کنم وقتی می خوام جواب بگیرم به ارور زیر بر می خورم.
ممنون میشم جوابی بدین.




public class ContactRepository
{
private const string CashKey = "ContactStrore";


public ContactRepository()
{
var ctx = HttpContext.Current;


if (ctx != null)
{
if (ctx.Cache[CashKey] == null)
{
var contacts = new Contact[]
{
new Contact
{
Id = 1, Name = "Glenn Block"
},
new Contact
{
Id = 2, Name = "Dan Roth"
}
};


ctx.Cache[CashKey] = contacts;
}
}


}


public Contact[] GetallContacts()
{
var ctx = HttpContext.Current;


if (ctx != null)
{
return (Contact[])ctx.Cache[CashKey];
}


return new Contact[]
{
new Contact
{
Id = 0,
Name = "Placeholder"
}
};


}


public bool SaveContact(Contact contact)
{
var ctx = HttpContext.Current;
if (ctx != null)
{
try
{
var currentData = ((Contact[])ctx.Cache[CashKey]).ToList();
currentData.Add(contact);
ctx.Cache[CashKey] = currentData.ToArray();
return true;
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
return false;
}
}
return false;
}
}




این هم کنترلر API من


private ContactRepository _contact;


public ContactController(ContactRepository contact)
{
_contact = contact;
}


public Contact[] Get()
{
return this._contact.GetallContacts();
}


کد HTML:
This XML file does not appear to have any style information associated with it. The document tree is shown below.      <Error><Message>An error has occurred.</Message><ExceptionMessage>An error occurred when trying to create a controller of type 'ContactController'. Make sure that the controller has a parameterless public constructor.</ExceptionMessage><ExceptionType>System.InvalidOperationException</ExceptionType><StackTrace>   at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)   at System.Web.Http.Controllers.HttpControllerDescriptor.CreateController(HttpRequestMessage request)   at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__1.MoveNext()</StackTrace><InnerException><Message>An error has occurred.</Message><ExceptionMessage>Type 'TestApi.Web.Controllers.ContactController' does not have a default constructor</ExceptionMessage><ExceptionType>System.ArgumentException</ExceptionType><StackTrace>   at System.Linq.Expressions.Expression.New(Type type)   at System.Web.Http.Internal.TypeActivator.Create[TBase](Type instanceType)   at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.GetInstanceOrActivator(HttpRequestMessage request, Type controllerType, Func`1& activator)   at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)</StackTrace></InnerException></Error>