Search This Blog

2023-04-19

.NEt Core 6- Routing

 Routing is a process of matching incoming HTTP requests by checking the HTTP method and url; and then invoking corresponding endpoints (middlewares).


Example

Program.cs

 //routing with default parameter with constraint

    endpoints.Map("employee/profile/{employeename:minlength(3):maxlength(5)=Manab}", async (context) =>

    {

        string? employeename = Convert.ToString(context.Request.RouteValues["employeename"]);

        await context.Response.WriteAsync($"In Profle-{employeename}");

    });


    //routing with optional parameter with contraint

    endpoints.Map("products/details/{id:int?}", async (context) =>

    {

        if (context.Request.RouteValues.ContainsKey("id"))

        {

            int id = Convert.ToInt32(context.Request.RouteValues["id"]);

            await context.Response.WriteAsync($"In produts-{id}");

        }

        else

            await context.Response.WriteAsync($"In produts-id not supplied");

    });

No comments: