Adding Web API to existing asp.net web forms Application

前端 未结 2 1769
遥遥无期
遥遥无期 2020-12-13 20:19

My team wants to upgrade from WCF to Web API. We have a working asp.net web form application, that we have imported to VS2012 from VS2010. So far so good.

But now a

相关标签:
2条回答
  • 2020-12-13 20:54

    After much research I have been able to come up with a solution to this problem. Let me illustrate my solution with respect to the Visual Studio Version.

    VS 2012

    As I mentioned in the question, there is no definite way to create the Web API project in VS2012. You are gonna have to do it by creating an MVC 4 application and setting the Project Template as WebAPI. Then once you have done this and you have your Web API functional, you can safely delete the extra baggage like the Jquery libraries and other stuff, because these things are absolutely of no use here in your project.

    VS 2013

    In VS2013 there is however a better approach followed to add and manage the Web API projects. Currently I am using VS2013 for the Web API and all things have fallen into place just as I wanted. Kindly see this link and you will get a better idea

    http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api

    I hope this information will help all those new to Web API. Especially for those who want to upgrade to Web API or add Web API to existing projects.

    0 讨论(0)
  • 2020-12-13 20:55

    In Visual Studio 2013

    1. Right-click on the ASP.NET Web Forms project.
    2. Add -> Add Scaffolded Item...
    3. Under Installed/Common/MVC/Web API choose the scaffold type you wish to use.
    4. Follow the instructions for the scaffold template. For example, when you choose "Web API 2 Controller with read/write actions" or "Web API 2 Controller - Empty", you are prompted for a controller name
    5. You will then need to move the recently created controller into the recently created Controllers folder.

    Results

    From what I can see, Visual Studio does the following:

    • "App_Start/WebApiConfig2.cs" is created.
    • Controllers folder is created.
    • Web.config is edited to add "handlers" element with content in "system.webServer".
    • The following references are added:
      • System.Net.Http
      • System.Net.Http.Formatting
      • System.Web.Extensions
      • System.Web.Http
      • System.Web.Http.WebHost
    • packages.config is updated to include:
      • "Microsoft.AspNet.WebApi"
      • "Microsoft.AspNet.WebApi.Client"
      • "Microsoft.AspNet.WebApi.Core"
      • "Microsoft.AspNet.WebApi.WebHost"

    Notes

    Subsequently, I recommend following the same steps, starting with right-clicking on the Controllers folder instead of the project. This will put the new controller in the Controllers folder instead of at the root level.

    Readme from Visual Studio after following the above steps:

    Visual Studio has added the full set of dependencies for ASP.NET Web API 2 to project 'RowersCode.Web'.

    The Global.asax.cs file in the project may require additional changes to enable ASP.NET Web API.

    1. Add the following namespace references:

      • using System.Web.Http;
      • using System.Web.Routing;
    2. If the code does not already define an Application_Start method, add the following method:

      protected void Application_Start()

      {

      }

    3. Add the following lines to the beginning of the Application_Start method:

      GlobalConfiguration.Configure(WebApiConfig2.Register);

    0 讨论(0)
提交回复
热议问题