Free Code Snippets in C#, Net Framework, Office 365, ASP.Net,WPF, Visual Studio, SQL Server, Antivirus free
#

 

If you need an external interface of devices to a web application, then you add an API controller to the web application, which then devices such as mobile phones, IoT devices, WPF desktop applications and other apps can exchange data directly.

The easiest way to create Web API interfaces is to use the ASP.Net template:

API Controller with actions using Entity Framework

 

keywords:

Rest Restful interface

 

Under the folder / controller creates a subfolder with / controller / Api.

Then via the context menu: Add Scaffold Item

And here's the selection: API Controller wit actions using Entity Framework

To API controller with REST actions to create, read, update, delete and list entities from Entity Framework data context. (by Microsoft v1.0.0.0)

 

In the dialog: Add API Controller with actions using Entity Framework

If you choose Model Class the data class or table, which you want to make externally accessible. Here the Model Class: Projects (Freelancer.Models)

Under Data context class: enter the database context interface. Here app_Database_Context (Freelancer)

 

 

Result:

 

This automatically creates the API interface with all 4 essential basic accesses

// GET: api/Projects Gettbl_Projects()

 

// GET: api/Projects/5 GetProject([FromRoute] int id)

 

// PUT: api/Projects/5 PutProject([FromRoute] int id, [FromBody] Project project)

 

// POST: api/Projects PostProject([FromBody] Project project)

 

// DELETE: api/Projects/5 DeleteProject([FromRoute] int id)

 

 

 

 

Code as WebApi for the Projects table

using System;

using System.Collections.Generic;

using System.Linq;

using System.Threading.Tasks;

using Microsoft.AspNetCore.Http;

using Microsoft.AspNetCore.Mvc;

using Microsoft.EntityFrameworkCore;

using Freiberufler;

using Freiberufler.Models;

 

namespace Freiberufler.Controllers.Api

{

    [Produces("application/json")]

    [Route("api/Projects")]

    public class ProjectsController : Controller

    {

        private readonly app_Database_Context _context;

 

        public ProjectsController(app_Database_Context context)

        {

            _context = context;

        }

 

        // GET: api/Projects

        [HttpGet]

        public IEnumerable<Project> Gettbl_Projects()

        {

            return _context.tbl_Projects;

        }

 

        // GET: api/Projects/5

        [HttpGet("{id}")]

        public async Task<IActionResult> GetProject([FromRoute] int id)

        {

            if (!ModelState.IsValid)

            {

                return BadRequest(ModelState);

            }

 

            var project = await _context.tbl_Projects.SingleOrDefaultAsync(m => m.ID_Project == id);

 

            if (project == null)

            {

                return NotFound();

            }

 

            return Ok(project);

        }

 

        // PUT: api/Projects/5

        [HttpPut("{id}")]

        public async Task<IActionResult> PutProject([FromRoute] int id, [FromBody] Project project)

        {

            if (!ModelState.IsValid)

            {

                return BadRequest(ModelState);

            }

 

            if (id != project.ID_Project)

            {

                return BadRequest();

            }

 

            _context.Entry(project).State = EntityState.Modified;

 

            try

            {

                await _context.SaveChangesAsync();

            }

            catch (DbUpdateConcurrencyException)

            {

                if (!ProjectExists(id))

                {

                    return NotFound();

                }

                else

                {

                    throw;

                }

            }

 

            return NoContent();

        }

 

        // POST: api/Projects

        [HttpPost]

        public async Task<IActionResult> PostProject([FromBody] Project project)

        {

            if (!ModelState.IsValid)

            {

                return BadRequest(ModelState);

            }

 

            _context.tbl_Projects.Add(project);

            await _context.SaveChangesAsync();

 

            return CreatedAtAction("GetProject", new { id = project.ID_Project }, project);

        }

 

        // DELETE: api/Projects/5

        [HttpDelete("{id}")]

        public async Task<IActionResult> DeleteProject([FromRoute] int id)

        {

            if (!ModelState.IsValid)

            {

                return BadRequest(ModelState);

            }

 

            var project = await _context.tbl_Projects.SingleOrDefaultAsync(m => m.ID_Project == id);

            if (project == null)

            {

                return NotFound();

            }

 

            _context.tbl_Projects.Remove(project);

            await _context.SaveChangesAsync();

 

            return Ok(project);

        }

 

        private bool ProjectExists(int id)

        {

            return _context.tbl_Projects.Any(e => e.ID_Project == id);

        }

    }

}

 

Mobile
»
🚩#23 ASP.Net: Add Web API Controller
»
🚩#11 Create and Connect a database to ASP.Net MVC Core 2
»
🚩# 23 ASP.Net: Adjust Validation
»
🚩#22 ASP.Net: Validation, validity
»
🚩#21 ASP.Net: Update Data, Save Edit View
»
# 20 Bind form data and attach to database
»
ASP.Net Core MVC: Insert dropdown list
»
ASP.Net: adapt the label
»
#17 ASP MVC: New form for creating an entry
»
EF: Specify default values ​​and Type Double and Bool from ASP.Net Model to SQL Server

.

Contact for Jobs, Project Requests: raimund.popp@microsoft-programmierer.de