Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting up Web API within WCF Project

I have a little bit of a "strange practise" question. The requirement on an architecture of our project is to setup Web API with (if possible) all MVC goodness within WCF project. That means that WCF and Web API web services would be stood up along each other within one project.

Is this even doable? I've given it a short go and found that even merging the web configs of two projects is very hard.

Thank you for your suggestions and comments,

Jakub

like image 906
Jakub Holovsky Avatar asked Aug 13 '13 08:08

Jakub Holovsky


1 Answers

I followed these steps and it worked fine:

  1. Make sure your WCF service is working correctly.
  2. Install WebAPI to your project through Nuget Package Manager

    Install-Package Microsoft.AspNet.WebApi

  3. Create Controller folder and write your controller class and methods.

  4. Create global.asax file
  5. Register routes for your services in Application_Start method.

    protected void Application_Start(object sender, EventArgs e)
    { 
        RegisterRoutes(RouteTable.Routes);
    }
    
    private void RegisterRoutes(RouteCollection routes)
    {
        routes.MapHttpRoute(
    
             "webapi_route",
    
              "/{controller}/{action}/{id}",
    
             new { controller = "controller_name", action = "method_name", id = RouteParameter.Optional }
    
       );
    
        RouteTable.Routes.Add(new ServiceRoute("", new WebServiceHostFactory(), typeof(service_name)));
    }
    
like image 78
Nameless Avatar answered Nov 16 '22 02:11

Nameless