添加Web API,以现有的asp.net Web窗体应用程序 [英] Adding Web API to existing asp.net web forms Application

查看:252
本文介绍了添加Web API,以现有的asp.net Web窗体应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的团队希望从升级到WCF的Web API。我们有一个工作asp.net web表单应用程序,我们已经导入到VS2012从VS2010。到现在为止还挺好。

但现在我尽量让一个单独的Web API项目,我看到没有可用的Web API模板。 我能找到最接近的是通过创建一个MVC 4应用程序,并设置项目模板的WebAPI 。我跟着这样,一切都完美地落在英寸我有一个样品控制器,我可以从浏览器拨打电话调用API的工作。

唯一的缺点这是,这个特殊的方法在自己的行李带来的。我创建了MVC 4项目有jQuery和其他库收录,再加上,我可能并不需要一些其他文件夹。我要的是Web API结构只 - 而不是微胖

我试图找到利用网上搜索,但我发现不能正常工作和非常差的评价包装的模板。

我希望我已经正确说明我的问题。我期待着一些反馈,现在:)谢谢。


解决方案

在Visual Studio 2013


  1. 右键单击ASP.NET Web窗体项目。

  2. 添加 - > 添加脚手架项...

  3. 在安装/通用/ MVC /网络API选择您想要的支架型
    使用。

  4. 按照该脚手架模板的说明。举例来说,当你选择网页API 2控制器具有读/写操作或网页API 2控制器 - 空,系统会提示您一个控制器名称

  5. 您一定要去最近创建的控制器移动到最近创建的控制器文件夹中。

结果

这是我所看到的,Visual Studio中执行以下操作:


  • App_Start / WebApiConfig2.cs 创建。

  • 控制器文件夹中创建。

  • 的Web.config 编辑加上处理元素在system.webServer的内容。

  • 下的引用添加:

    • System.Net.Http

    • System.Net.Http.Formatting

    • System.Web.Extensions程序

    • System.Web.Http

    • System.Web.Http.WebHost


  • packages.config 已更新,包括:

    • Microsoft.AspNet.WebApi

    • Microsoft.AspNet.WebApi.Client

    • Microsoft.AspNet.WebApi.Core

    • Microsoft.AspNet.WebApi.WebHost


备注

随后,我建议按照相同的步骤,开始在控制器上的文件夹,而不是项目上右击。这将使新的控制器Controllers文件夹,而不是在根级别。

从Visual Studio自述按照上述步骤后:


  

Visual Studio中加入了全套的依赖关系的ASP.NET Web API 2项目赛艇code.Web。


  
  

该文件Global.asax.cs中的项目可能需要额外的更改,使的ASP.NET Web API。


  
  

      
  1. 添加以下命名空间引用:


      
      

        
    • 使用System.Web.Http;

    •   
    • 使用System.Web.Routing;

    •   

  2.   
  3. 如果在code尚不定义Application_Start方法中,添加下面的方法:


      
      

    保护无效的Application_Start()


      
      

    {


      
      

    }


  4.   
  5. 以下行添加到Application_Start方法中的开头:


      
      

    GlobalConfiguration.Configure(WebApiConfig2.Register);


  6.   

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 as I try to make a separate Web API project, I see that there is no Web API template available. The closest thing that I can find is by creating an MVC 4 application and setting the Project Template as WebAPI. I followed this way and everything falls in perfectly. I have the working API with a sample controller that I can invoke by making calls from the browser.

The only downside to this is that, this particular method brings in its own baggage. The MVC 4 project I created has JQUERY and other libraries included, plus some other folders that I probably don't need. What I want is the Web API structure only - and not the extra baggage.

I tried finding a template using online search but the package I found does not work properly and as very poor rating.

I hope I have illustrated my problem properly. I am looking forward for some feedback now :) Thanks.

解决方案

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

这篇关于添加Web API,以现有的asp.net Web窗体应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆