In ASP.NEt MVC4 application Json Web api needs to be created to serve requests which can expressed using the urls like:
http://localhost:52216/erp/api/customers
return all customershttp://localhost:52216/erp/api/customers?term=soft
return list of customers whose same contains "soft". Used for autocomplete.
Result from those requests must be json object which contains single property , customers which contain array of customers found.
3.
post request to http://localhost:52216/erp/api/customers
should add new customer which is specified in request body as json
Result from this method must be json object which contains single property, customer which contain saved customer with some properties changed.
For this API controller below is tried to use.
Typing browser http://localhost:52216/erp/api/customers
returns error in xml format
<Error><Message>No HTTP resource was found that matches the request URI 'http://localhost:52216/erp/api/customers'.</Message>
<MessageDetail>No action was found on the controller 'Customers' that matches the request.</MessageDetail>
</Error>
How to fix this ? Which is propery way to crete API class for such request?
Request return data format cannot changed. Class method names can changed and separate methods with different names can created if required.
using Erp.Models;
using System.Web.Http;
namespace Erp.Controllers
{
[Authorize]
public class CustomersController : ApiController
{
public object Get(string term)
{
Customer[] res = CustomerRepository.GetAllOrForTerm(term);
return new { customers = res };
}
public object Post([FromBody]Customer customer)
{
Customer res = CustomerRepository.Save(customer);
return new { customer = res };
}
}
}
default routing is used:
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
Update
Application is running from erp virtual directory so removing it does not help.
I tried also in browser
http://localhost:52216/erp/api/customers/get
and
http://localhost:52216/erp/api/customers/Get
but got error
<Error>
<Message>
No HTTP resource was found that matches the request URI 'http://localhost:52216/erp/api/customers/get'.
</Message>
<MessageDetail>
No action was found on the controller 'Customers' that matches the request.
</MessageDetail>
</Error>