My mvc 4 application exposes an API layer for 3 different child applications. I am using a single api controller for all the three child apps. All these three apps uses the parent app DB.
I would like to know if i am doing anything wrong with this. Also, as the app develops, the api controller is becoming heavy. Is there any good way by which i can manage the child app in the parent app project?.
IHttpActionResult contains a single method, ExecuteAsync, which asynchronously creates an HttpResponseMessage instance. If a controller action returns an IHttpActionResult, Web API calls the ExecuteAsync method to create an HttpResponseMessage. Then it converts the HttpResponseMessage into an HTTP response message.
As mentioned, Web API controller can include multiple Get methods with different parameters and types. Let's add following action methods in StudentController to demonstrate how Web API handles multiple HTTP GET requests.
A HttpResponseMessage allows us to work with the HTTP protocol (for example, with the headers property) and unifies our return type. In simple words an HttpResponseMessage is a way of returning a message/data from your action. The following is a quick glimpse of that: // GetEmployee action.
Leverage action results to return data as an HttpResponseMessage object from your Web API controller method. ASP.Net Web API is a lightweight framework used for building stateless and RESTful HTTP services. You can take advantage of Action Results in Web API to return data from the Web API controller methods.
You can make use of Areas to manage your child apps in the parent one. Please follow steps answered in the question below to create areas in your project
How to Configure Areas in ASP.NET MVC3
For handling Api requests to areas, you need to have two routes in the area registration.
public override void RegisterArea(AreaRegistrationContext context)
{
context.Routes.MapHttpRoute(
name: "Area_Name_Api",
routeTemplate: "Area_Name/api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
context.MapRoute(
"Area_Name_default",
"Area_Name/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
}
The first route is for reaching api controller in the area and second one for regular ones.
http://blogs.infosupport.com/asp-net-mvc-4-rc-getting-webapi-and-areas-to-play-nicely/
The above link explains more on this.
By this way you can separate your child apps and organize your functions, models views(if any) in the parent project.
You can handle the child apps under different web api controllers.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With