惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
月光博客
月光博客
The Last Watchdog
The Last Watchdog
T
Tenable Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
C
CXSECURITY Database RSS Feed - CXSecurity.com
Simon Willison's Weblog
Simon Willison's Weblog
V
Vulnerabilities – Threatpost
F
Fortinet All Blogs
Microsoft Security Blog
Microsoft Security Blog
A
Arctic Wolf
云风的 BLOG
云风的 BLOG
Know Your Adversary
Know Your Adversary
P
Palo Alto Networks Blog
GbyAI
GbyAI
阮一峰的网络日志
阮一峰的网络日志
The GitHub Blog
The GitHub Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
U
Unit 42
MyScale Blog
MyScale Blog
B
Blog
Spread Privacy
Spread Privacy
S
Schneier on Security
Project Zero
Project Zero
L
LINUX DO - 热门话题
M
MIT News - Artificial intelligence
F
Full Disclosure
WordPress大学
WordPress大学
Apple Machine Learning Research
Apple Machine Learning Research
Cyberwarzone
Cyberwarzone
AWS News Blog
AWS News Blog
aimingoo的专栏
aimingoo的专栏
博客园 - 三生石上(FineUI控件)
C
Cybersecurity and Infrastructure Security Agency CISA
Hugging Face - Blog
Hugging Face - Blog
Security Latest
Security Latest
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
T
Tailwind CSS Blog
K
Kaspersky official blog
Recent Announcements
Recent Announcements
NISL@THU
NISL@THU
Cisco Talos Blog
Cisco Talos Blog
S
Securelist
P
Privacy & Cybersecurity Law Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
T
The Exploit Database - CXSecurity.com
V
Visual Studio Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Webroot Blog
Webroot Blog

博客园 - 如是如是

将博客搬至CSDN 钱钟书夫人杨绛:《一百岁感言》 DOM无关事件 用到了反射,还不错。可以取到动态创建类的值 JounceTabRegion Jounce Blendable RestShart Post 方式传递poco给Web api How to Migrate from WCF Web API to ASP.NET Web API ASP.NET Web API MVC-REST-SilverLight 之 架构 MVC-REST-SilverLight 之 RestExample.Model.Silverlight\Customer.cs - 如是如是 MVC-REST-SilverLight 之MainPage.xaml.cs MVC-REST-SilverLight 之 ViewModels\MainViewModel.cs - 如是如是 MVC-REST-SilverLight 之Api\CustomerApi.cs - 如是如是 - 博客园 MVC-REST-SilverLight 之 Global.asax.cs MVC-REST-SilverLight 之 HttpConfiguration MVC-REST-SilverLight 之 MapServiceRoute MEF Export 和 Import 委托 设计模式-访问者
Using ASP.NET Web API with ASP.NET Web Forms
如是如是 · 2012-03-07 · via 博客园 - 如是如是

MSDN Blogs > Henrik's Blog > Using ASP.NET Web API with ASP.NET Web Forms

Using ASP.NET Web API with ASP.NET Web Forms

Rate This

Henrik F Nielsen

Henrik F Nielsen

MSFT

2,575

Recent Achievements 9 2 0

Blog Conversation Starter Gallery Contributor II New Blogger

View Profile

23 Feb 2012 1:30 PM

  • Comments 5

Several of the ASP.NET Web API tutorials (see for example "Your First ASP.NET Web API") show how you can use ASP.NET Web API with MVC applications. However, you can equally well add a Web API to a Web Form enabling the same Web API support as in MVC applications.

To show how you do this, let's build a Web Site from scratch and then add a Web API to it. We use the Visual Web Developer 2010 Express version of Visual Studio with ASP.NET Web API Beta installed.

First we create a new ASP.NET Empty Web Site and give it a reasonable name:

Then we add an ASP.NET Folder to hold our Web API implementation. We do this by adding an App_Code folder as follows:

Next we add a Web API Controller Class within the App_Code folder using Add New Item.

Note: Make sure the name ends in "Controller" and not "Controller1" or similar.

The newly created ValuesController illustrates a very basic Web API with support for HTTP GET, POST, PUT, and DELETE.

Note: You may have to fix the namespace so that it doesn't start with a "dot "." (this is a bug):

Let's leave the default ValuesController for now and instead add a route to the global route table so that we can route messages to the Web API.

To do this, open the file Global.asax in the project. Here we need to add two things:

  1. First we add a couple of import statements for System.Web.Routing and System.Web.Http to get the right name places
  2. Second we add a route entry in the global route table in the Application_Start method

The two edits together look like this:

1: <%@ Application Language="C#" %>

2: <%@ Import Namespace="System.Web.Routing" %>

3: <%@ Import Namespace="System.Web.Http" %>

4: 

5: <script runat="server">

6: 

7: void Application_Start(object sender, EventArgs e)

8: {

9: RouteTable.Routes.MapHttpRoute(

10: name: "DefaultApi",

11: routeTemplate: "api/{controller}/{id}",

12: defaults: new { id = System.Web.Http.RouteParameter.Optional }

13: );

14: }

You are now ready to try it out so hit F5 and start it!

To see what is going on you can use your browser to send a request to your controller. Typically you would use JavaScript AJAX calls (see for example the tutorial "Your First ASP.NET Web API") to invoke the ApiController but for GET you can also just use your browser and point it at the address "api/values" relative to the base address of your Web Site.

For example, if your Web Site is running as

then your values controller will be answering at

In IE the default response will be returned as application/json which IE will ask whether you want to open or save. If you save the result and open it then see the JSON result

  • ["value1","value2"]

Alternatively, if you use the address

then you will see only the first value

  • "value"

That's it – you now have a Web API running in your Web Site.

Have fun!

Henrik

del.icio.us Tags: asp.net,web,mvc,webapi,rest