As already mentioned in Solution 1, by MadMyche, you cannot simply convert your applications to an API, where API itself is a separate way or method of development of applications. An API has so many requirements, such as being stateless, headless, and cross-platform, and cross-device. APIs work directly on the HTTP protocol, and do not require a browser to display the content. In this fashion, the Views etc won't be needed.
Representational state transfer - Wikipedia[
^]
This way, you would be using a data-interchange format, like JSON or XML, and then return the data using these formats, and then access that from the client. So here is the thing, you need to change the View to the Models—
you return the data instead of an HTML document. Then that data can be accessed anywhere, and parsed.
Please see this article of mine to understand how this can happen,
ASP.NET 5 Web API RESTful CRUDs and Windows 10 native application[
^]
Your code, would be changed to the following code,
[HttpPost]
public Employee Create()
{
var model = new Employee();
if (id == null)
{
model.EmployeeId = _repository.GetAll().Max(Employee => Employee.EmployeeId) + 1;
}
return model;
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<Employee> Create(Employee employee)
{
_context.Add(employee);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
return employee;
}
Here is another interesting thing, since you are using an API, your users can upload the data using any method, query string, POST it via HTTP body, send a file, send as form-data, whatever. You need to check the data via those methods, such as
[FromBody]
, etc.
Model Binding in ASP.NET Core | Microsoft Docs[
^]