Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mvc 4 web api for multiple applications

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?.

like image 993
Tanmay Bose Avatar asked Mar 17 '13 03:03

Tanmay Bose


People also ask

What is difference between IHttpActionResult and HttpResponseMessage?

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.

Can we have multiple get methods in Web API?

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.

What is HttpResponseMessage in Web API?

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.

What is Actionresult in Web API?

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.


2 Answers

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.

like image 109
Rifaj Avatar answered Nov 03 '22 00:11

Rifaj


You can handle the child apps under different web api controllers.

like image 25
SHIJIL P Avatar answered Nov 02 '22 23:11

SHIJIL P