Reputation: 9866
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.
Upvotes: 0
Views: 1203
Reputation: 12341
Just to clarify the previous answer about not being able to create a Web Api
project without MVC. You can absolutely do that and get what you want (a Web API only application):
After doing the above (which will already have the necessary default scaffolding), and copying your model, the only change to your controller:
public class UserController : ApiController
{
//Handle GET request to /api/{controller name}
public List<User> Get()
{
return new List<User> { new User { Name = "Peter", Age = 41 } };
}
}
It's a Web API application so you won't have/need Views
.
To invoke the above, via browser, browse to:
http://localhost:123456/api/user
Response:
<ArrayOfUser xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/StandAloneWebApi.Models">
<User>
<Age>41</Age>
<Name>Peter</Name>
</User>
</ArrayOfUser>
It's XML because I invoked it via a browser and am not "asking" for a specific response. If I set the Accept
header to application/json
I will get JSON.
e.g. using curl
curl -H "accept: application/json" http://localhost:123456/api/user
Response (edited for brevity):
< HTTP/1.1 200 OK
< Pragma: no-cache
< Content-Type: application/json; charset=utf-8
....
[{"Name":"Peter","Age":41}]
The link in the Aleksandr Ivanov's answer will be very helpful and will give insight to how Web Api handles/routes requests.
Hth...
Upvotes: 1
Reputation: 2786
I think it's totally valid response from the server because you don't have any routes configured. Routing is configured differently for MVC and Web API. You can read more about Web API routing here.
I think the easiest option for you will be to create a Web API project from a template. In VS2013 when you create new web ASP.NET Web Application you can choose which template to use. It's not possible to select only Web API without MVC. After you create a project you can see that there are two files in App_Start
folder: WebApiConfig
and RouteConfig
. RouteConfig
is routing for MVC. You can just delete this file and it's usage in Global.asax
.
EDIT: As @EdSF mentioned you actually can create Web API project without MVC. Just select Empty
template and check Web API check-box.
So your WebApiConfig
will look like this:
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
MapHttpAttributeRoutes
is used to make attribute routing work and MapHttpRoute
is similar to MVC. As you can see there is an api
prefix, it's up to you if you want to use it or not. In general it's not a bad idea. For example if in the future you will have second version of your API you and you will need to keep first version too, then you can add new prefix apiv2
or something like that.
If you don't like 403.14 response for root you can configure global handling for such situations. Read this question for example.
Upvotes: 1