Map, MapGet, MapPost, MapPut and MapDelete methods in ASP.NET Core MVC - Routing ASP.NET

 namespace MapMethodsRouting

{

    public class Program

    {

        public static void Main(string[] args)

        {

            var builder = WebApplication.CreateBuilder(args);

            var app = builder.Build();


            app.UseRouting();


            app.UseEndpoints(endpoints => 

            {

                endpoints.MapGet("/Home", async (context) =>

                {

                    await context.Response.WriteAsync("This is Home Page... GET");

                });

                endpoints.MapPost("/Home", async (context) =>

                {

                    await context.Response.WriteAsync("This is Home Page... POST");

                });

                endpoints.MapPut("/Home", async (context) =>

                {

                    await context.Response.WriteAsync("This is Home Page... PUT");

                });

                endpoints.MapDelete("/Home", async (context) =>

                {

                    await context.Response.WriteAsync("This is Home Page... DELETE");

                });

            });


            app.Run(async (HttpContext context) =>

            {

                await context.Response.WriteAsync("Page Not Found...");

            });


            //Below is called "Request Delegate" which is defined as shown above :

            //async (context) =>

            //{

            //    await context.Response.WriteAsync("This is Home Page... DELETE");

            //}


            //app.MapGet("/", () => "Hello World!");

            //app.Map("/Home", () => "Hello World!"); //By default, GET request works

            //app.MapGet("/Home", () => "Hello World! - GET");

            //app.MapPost("/Home", () => "Hello World! - POST");

            //app.MapPut("/Home", () => "Hello World! - PUT");

            //app.MapDelete("/Home", () => "Hello World! - DELETE");


            app.Run();

        }

    }

}



Comments

Popular posts from this blog

Layout View In ASP.NET Core 6 | _Layout.cshtml ASP.NET | Master Page ASP.Net Core

Attribute Routing in ASP.NET Core MVC