Tech Junkie Blog - Real World Tutorials, Happy Coding!: ACME Bank: Step 6: Running Your Asp.Net Core Application For The First Time

Tuesday, September 15, 2020

ACME Bank: Step 6: Running Your Asp.Net Core Application For The First Time

Before we run our Asp.Net Core application for the first time we are going to copy a controller from Asp.Net Core 2.2 to help us create the skeleton for our application.  A skeleton are the plumbings we do at the beginning of our application to make sure all the mechanisms work.  It's called the ValuesController which has been replaced with a more complicated WeatherForecastController.  The ValuesController is just a lot easier to work with to get things set up.


Just create a new controller in the "Controllers" and call it "ValuesController.cs" Here is the code for it.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;

namespace ACMEBank.API.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class ValuesController : ControllerBase
    {
        // GET api/values
        [HttpGet]
        public ActionResult> Get()
        {
            return new string[] { "value1", "value2" };
        }

        // GET api/values/5
        [HttpGet("{id}")]
        public ActionResult Get(int id)
        {
            return "value";
        }

        // POST api/values
        [HttpPost]
        public void Post([FromBody] string value)
        {
        }

        // PUT api/values/5
        [HttpPut("{id}")]
        public void Put(int id, [FromBody] string value)
        {
        }

        // DELETE api/values/5
        [HttpDelete("{id}")]
        public void Delete(int id)
        {
        }
    }
}

As you can see it's pretty basic.

Before we run our application for the first time we want to comment out the redirect https and keep our application in http mode in the beginning so that it's easier to work with. We also need to remove the https port in the launchSettings.json file, in Properties/launchSettings.json

Here is where you comment out the https redirect in the Startup.cs file

       // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)

        {

            if (env.IsDevelopment())

            {

                app.UseDeveloperExceptionPage();

            }



            // app.UseHttpsRedirection();



            app.UseRouting();



            app.UseAuthorization();



            app.UseEndpoints(endpoints =>

            {

                endpoints.MapControllers();

            });

        }

Here is the change in launchSettings.json, make sure you remove the https: portion of the URL in the last configuration and just leave the http://localhost:5000

    "ACMEBank.API": {

      "commandName": "Project",

      "launchBrowser": true,

      "launchUrl": "weatherforecast",

      "applicationUrl": "http://localhost:5000",

      "environmentVariables": {

        "ASPNETCORE_ENVIRONMENT": "Development"

      }

Now you are ready to run the Asp.Net Core api by openning a terminal session and type dotnet run

Just open up the browser and browse to http://localhost:5000/api/values we would get the values from our HttpGet method of the ValuesController








Congratulation! You've just created your first API!

Previous: ACME Bank: Step 5: Setup Visual Code For C# Development With Extensions

Next: Add A Dev User In MySQL

4 comments:

  1. Your blog is very informative, finally, I found exactly what I want. Paypal is an excellent service for online payments but lots of its users confront issues while they access Paypal. If you want to resolve your problems then must visit Paypal contact Nederland.

    ReplyDelete
  2. Your blog is very informative and interesting to read, finally, I found exactly what I searching for. There are lots of users of Macfee antivirus in the world because of its features and easy interface. If you want to explore more interesting facts about Mcafee antivirus or want to resolve your technical issues then must visit Mcafee klantenservice nummer.

    ReplyDelete

Search This Blog