C# HTTP Examples
C# Basic
C# Data Type
C# OOP
C# IO
C# Graphics & UI
C# Advanced
To call a RESTful service API in C#, you can use the HttpClient class. Here is an example:
using System.Net.Http; using System.Threading.Tasks; public static async Task<string> CallRestApi(string url) { using (HttpClient client = new HttpClient()) { HttpResponseMessage response = await client.GetAsync(url); response.EnsureSuccessStatusCode(); string responseBody = await response.Content.ReadAsStringAsync(); return responseBody; } }
To pass an array of values in ASP.NET Web API, you can use the [FromUri] attribute to bind the values from the query string to an array parameter in your action method. Here is an example:
[HttpGet] public IHttpActionResult MyActionMethod([FromUri] int[] values) { // do something with the values array }
To ignore a specific object property during JSON serialization in Web API, you can use the [JsonIgnore] attribute on the property. Here is an example:
public class MyModel { public int Id { get; set; } [JsonIgnore] public string Secret { get; set; } public string Name { get; set; } }
In this example, the "Secret" property will be ignored during JSON serialization.
C# Web API authentication in ASP.NET:
[Authorize] public class ValuesController : ApiController { // Controller actions }
ASP.NET Web API routing in C#:
config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } );
Securing ASP.NET Web API endpoints in C#:
[Authorize(Roles = "Admin")] public IHttpActionResult SecureEndpoint() { // Endpoint logic }
ASP.NET Web API versioning in C#:
config.Services.Replace(typeof(IHttpControllerSelector), new VersionedControllerSelector(config));
C# Web API parameter binding:
public IHttpActionResult Get(int id) { // Retrieve data based on the id parameter }
Working with JSON in ASP.NET Web API:
public IHttpActionResult Post([FromBody] MyModel data) { // Process JSON data }
C# Web API error handling:
public IHttpActionResult Get() { try { // Logic that may throw an exception } catch (Exception ex) { return InternalServerError(ex); } }
ASP.NET Web API CORS in C#:
config.EnableCors(new EnableCorsAttribute("*", "*", "*"));
Token-based authentication in ASP.NET Web API:
[Authorize] public IHttpActionResult SecureEndpoint() { // Access secured endpoint }
C# Web API model validation:
public IHttpActionResult Post([FromBody] MyModel data) { if (!ModelState.IsValid) { return BadRequest(ModelState); } // Process valid data }
Implementing CRUD operations in ASP.NET Web API:
public IHttpActionResult Get(int id) { // Retrieve data based on the id parameter } public IHttpActionResult Post([FromBody] MyModel data) { // Create new data } public IHttpActionResult Put(int id, [FromBody] MyModel data) { // Update data based on the id parameter } public IHttpActionResult Delete(int id) { // Delete data based on the id parameter }
Dependency injection in ASP.NET Web API:
public class ValuesController : ApiController { private readonly IService _service; public ValuesController(IService service) { _service = service; } }
C# Web API custom filters:
public class CustomFilterAttribute : ActionFilterAttribute { // Custom filter logic }
Securing ASP.NET Web API with OAuth in C#:
[Authorize] public IHttpActionResult SecureEndpoint() { // Access secured endpoint }