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

 

 

To check the data of the API interface, you now have direct access via the URL / Api

Instead of the address in the browser

localhost: 51081 / Projects

to enter for the http website in the browser,

 

Now give the URL

localhost: 51081 / API / Projects

and then receives the results in text form

 

Microsoft ASP automatically prepares the data itself

[{"iD_Project":1,"url":"http://microsoft.de/","title":"title1","text":"text 1"},{"iD_Project":2,"url":"https://Microsoft-Programmierer.de","title":"Title 2","text":"Text 2"}]

 

 

 

The attribute Produces ("application / json) before the API class automatically sets the output to Json as the output format.

[Produces("application/json")]

[Route("api/Projects")]

    public class ProjectsController : Controller

    {..

 

 

 

 

For better testing you should use extensions in the browser.

Here it is often recommended to integrate the Chrome browser extension Postman

 

The tool can be found at the Chrome Shop:

"Tabbed Postman - REST Client"

Watch: https://chrome.google.com/webstore/detail/tabbed-postman-rest-clien/coohjcphdfgbiolnekdpbcijmhambjff?utm_source=gmail

 

No separate window required! This is the legacy postman app that can run in a Chrome tab originally developed by…

No separate window required!

 

This is the legacy postman app that can run in a Chrome tab originally developed by https://www.getpostman.com/.

 

* Now runs in tab.

* Does not present itself as a Chrome App.

* Does not open a new window.

* Accessed by clicking the Postman icon in the Chrome toolbar.

 

Forked from https://github.com/postmanlabs/postman-chrome-extension-legacy

 

 

Include with Add in the Google Chrome browser

 

Now if you enter the test URL like

http://localhost:51081/api/Projects

then comes a geliederte return

 

 

Add in client

To add one enters then as Type: POST and the URL address on the Model Class

/ API / Projects

POST: http://localhost:51081/api/Projects

Additionally, you have to insert Content-Type: application / json in the header area.

In the output area of ​​Postman, the result is output with status: 201 Created.

 

The client sends the following text:

POST /api/Projects HTTP/1.1
Host: localhost:51081
Content-Type: application/json
Cache-Control: no-cache

{ "url": "http://microsoft.de/", "title": "title 2 geändert", "text": "text 1" }

 

 

Create, Add under ASP MVC

Under ASP.Net MVC Api the Create area is correct.

MVC takes over the standard routing through the attibut [HttpPost] and the standard designation Postxxxx

        // 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);

        }

 

Result on the SQL server.

The SQL Server Object Explorer shows that the results have been added.

Attention: it was apparently the ID increased incorrectly.

Mobile
»
Postman: Check API Controller with Chrome Extension
»
ASP.Net Core: Bootswatch Themes
»
ASP.Net Core: Install recommended additional tools

.

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