Categories
.NET Core

Listing all routes in ASP.NET Core application

It is pretty simple to list all routes configured in your ASP.NET Core application:

  1. Add Microsoft.AspNetCore.Mvc.Infrastructure.IActionDescriptorCollectionProvider parameter to your Startup.Configure method.
  2. If you have not done so already also add the ILogger<Startup> parameter to Startup.Configure as well
  3. Loop through all action descriptor items in the IActionDescriptorCollectionProvider and display the log information

Here is an example Startup method:

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILogger log, Microsoft.AspNetCore.Mvc.Infrastructure.IActionDescriptorCollectionProvider actionProvider)
    {
        log.Log(LogLevel.Information, "Configuring application ...");

        /// .... configuration

        app.UseMvc();

        log.Log(LogLevel.Information, "Available routes:");
        var routes = actionProvider.ActionDescriptors.Items.Where(x => x.AttributeRouteInfo != null);
        foreach(var route in routes)
        {
            log.Log(LogLevel.Debug, $"{route.AttributeRouteInfo.Template}");
        }

        log.Log(LogLevel.Information, "... finished configuring application");
    }
Categories
.NET Programming

Mixing string verbatim (@) and string interpolation ($) characters

It is possible to combine the string verbatim (@) and string interpolation ($) characters when entering string literals in your C# code. In order to do that you must first use the $ character followed by @ character.
Example:

int id = 1;
var result = await _con.QueryAsync(
                $@"select *
                    from   A
                    where A.ID = {id}");