Get the App
SLTechnology News&Howtos  ›  Development  › 

The way to call Web API in ASP.NET

Shulou Source: shulou.com Published: 2022-06-03 17:38:27 09月15日 Update

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!

Tags: Data name method content that is file route configuration superscript subscript value background instance customer client beginning method more friends architecture Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology MariaDB Docker Redmi MySQL