Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The way to call Web API in ASP.NET

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces the way of calling Web API in ASP.NET, which has certain reference value and can be used for reference by friends who need it. Let's take a look at it with me.

I. routing of Web API

1. Create a new MVC4 project in Visual Studio, and there is a WebApiConfig.cs file in the App_Start directory, which is the routing configuration of the corresponding Web API.

2. The Web API framework is based on the Restful architecture pattern by default. Unlike ASP.NET MVC, it looks for Action in the Http according to the HttpMethod (Get, Post, Put, Delete) of the Controller request. The rule is: does the Action name start with Get and Post? Marks on Action, HttpGet, HttpPost, etc.

3. Of course, you can modify the default configuration so that the client explicitly specifies the action name when calling, for example

Config.Routes.MapHttpRoute (name: "DefaultApi", routeTemplate: "api/ {controller} / {action} / {id}", defaults: new {id = RouteParameter.Optional})

In this way, because the Action name is explicitly specified, Web API uses that name to find the corresponding Action method instead of following the HttpMethod convention to find the corresponding Action.

2. A simple example of Web API in ASP.NET

1. Get request data

(1) define a UserModel class

Public class UserModel {public string UserID {get; set;} public string UserName {get; set;}}

(2) add a Web API Controller: UserController

Public class UserController: ApiController {public UserModel getAdmin () {return new UserModel () {UserID = "000000", UserName = "Admin"};}}

(3) access in the browser: api/user/getadmin (XML data model is returned by default)

(4) AJAX requests the api and specifies the data format as json

Ajax ({type: 'GET', url:' api/user/getadmin', dataType: 'json', success: function (data, textStatus) {alert (data.UserID + "|" + data.UserName);}, error: function (xmlHttpRequest, textStatus, errorThrown) {}})

2. POST submits data

(1) add an Action to UserController

Public bool add (UserModel user) {return user! = null;}

(2) add a button to the page

(3), JS post submit data

$('# btnOK') .bind ('click', function () {/ / create an ajax request and send the data to the background for processing var postData = {UserID:' 001 requests, UserName: 'QeeFee'}; $.ajax ({type:' POST', url: 'api/user/add', data: postData, dataType:' json', success: function (data, textStatus) {alert (data)) }, error: function (xmlHttpRequest, textStatus, errorThrown) {}});}). These are the details of how to call Web API in ASP.NET. Have you learned anything after reading it? If you want to know more about it, welcome to the industry information!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report