Setting up Web API within WCF Project

前端 未结 3 983
不思量自难忘°
不思量自难忘° 2021-01-12 13:32

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

3条回答
  •  粉色の甜心
    2021-01-12 13:39

    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)));
      }
      

提交回复
热议问题