For this new project we decied to use ASP.NET MVC 4
as our presentaional layer and WebApi 2
as service layer. The idea is - when the user interacts with the UI and some request is send, the MVC controller is recieving this call, and if some sort of information for the response, a call is made from the MVC controller to the WebApi
where the WebApi
is entirely different solution, and in theory it may not be on the same place physically with the MVC
solution.
For the MVC part I have this controller:
public async Task<ActionResult> Index()
{
return View("index", await GetUserAsync());
}
readonly string uri = "http://localhost:51120/api/user";
protected async Task<List<User>> GetUserAsync()
{
using (HttpClient httpClient = new HttpClient())
{
return JsonConvert.DeserializeObject<List<User>>(
await httpClient.GetStringAsync(uri)
);
}
}
Then I created an ASP.NET Empty Web Application
solution. When the solution was created I installed WebApi 2
from Nuget
so I had a pretty basic structure of my project. I manually created the Controllers
, Models
and Views
directories. In Controllers
I have one file :
public class UserController : ApiController
{
public List<User> GetStringAsync()
{
return new List<User> {new User { Name = "Peter", Age = 41}};
}
}
In Models
I have this :
public class User
{
public string Name { get; set; }
public int Age { get; set; }
}
And when I right click on the View
folder I chose to install an Empty view (without model)
. So basically that is my setup. When I start the solution I get this :
HTTP Error 403.14 - Forbidden The Web server is configured to not list the contents of this directory.
Most likely causes: A default document is not configured for the requested URL, and directory browsing is not enabled on the server.
And other stuff that I will post if neceessary. My question is - how can I do this. If I just create an MVC project with WebApi
template I guess everything will work fine but then the whole point of doint this is missing. I want to be able to host the WebApi
separately from the MVC solution, even physycally and only send and recieve calls.