• .Net Core,  Microsoft

    Dotnet Core Templates – The Revisit

    So a while back I penned an article (Dotnet new Magic), but now with the latest VS 2019 and Dotnet Core 3.x release… it’s time to revisit.

    In case we forgot – the command is:

    dotnet new -i “–The Template Reference Name Here–” 

    The list of templates can be found in the old place but also here – Dotnet New Templates

    What ones do I add:

    dotnet new –install “Microsoft.AspNetCore.SpaTemplates”
    dotnet new –install “Microsoft.Azure.WebJobs.ProjectTemplates”
    dotnet new –install “Microsoft.Azure.WebJobs.ItemTemplates”
    dotnet new –install “NUnit3.DotNetNew.Template”
    dotnet new –install “Microsoft.AspNetCore.Blazor.Templates”   **Some of these already exist, but this one adds the webassembly type.
    dotnet new –install “Microsoft.Azure.IoT.Edge.Module”
    dotnet new –install “Microsoft.Azure.IoT.Edge.Function”
    dotnet new –install “RaspberryPi.Template”

     

  • ASP.NET Core,  Microsoft

    ASP .Net Core Razor SPA vs. MVC Taghelpers Navigation

    As I mess about with deciding- do I want to do Razor SPA pages or do a full on ASP.Core MVC pages for my latest Web App? I noticed something when- Yes I put both in a solution- it comes to the ASP tag helper for navigation links…

    So here’s the thing. For a regular ASP.Core MVC we see tag helper navigations look like this:

    <a class="nav-link" asp-controller="Home" asp-action="Index">Home</span></a>

    Simple enough… “asp-controller”  has the simple controller name, and  “asp-action” the action within the controller… no problem

    Now consider Razor SPA or just simple razor pages:
     

    <a class="nav-link text-dark btn btn-outline-info" asp-page="/Index" data-toggle="tooltip" data-placement="top" title="Always Takes you back Home">Home</a>
    

    Here we have a “asp-page”.  Ok, I do get it… Razor SPA or Razor Pages do not follow the MVC pattern with a controller etc. But why do they need to be different? Yes I understand MVC came first and it is very understandable to have a clear navigation tag helper. But again, why not just one simple tag helper that sorts out where the page and/or controller is?

    This gets even more complex – mostly- when one considers “Areas”.

    Again very well defined for an MVC pattern. But to use within Razor Pages, you have to muck about with making sure this is available in your Startup.ConfigureServices. Why oh WHY?

    services.AddMvc()
            .AddRazorPagesOptions(options => options.AllowAreas = true);

    Maybe some day this will evaporate, but for now…  no.

    fyi… as this applies to ASP.Net Core 2.2, the doc reference is here:

    Anchor Tag Helper in ASP.NET Core

    Anchor Tag Helper in ASP.NET Core

    Discover the ASP.NET Core Anchor Tag Helper attributes and the role each attribute plays in extending behavior of the HTML anchor tag.

    Source: docs.microsoft.com/en-us/aspnet/core/mvc/views/tag-helpers/built-in/anchor-tag-helper?view=aspnetcore-2.2

     

    Happy tagging,
    ~SG

     

     

  • .Net Core,  Microsoft

    .Net Core ASP Tag Helpers

    I has to be one of the most useful things added… You got Tag Helps…

    Consider the simplest example…

    <img src="/images/somePng.png" />
    
    

    Not hard and very common. But consider the task of versioning your images for a web page… Enter Tag Helpers

    <img src="/images/somePng.png" asp-append-version = "true"/>

    This gives us a Hash version appended, so if an image changes? Indeed yes the hash value changes and this will trigger a cached page to update the image. 

    Wow, Tag Helpers… very handy.

    ScottGeek

     

  • ASP.NET Core,  Microsoft

    Using Kestrel for ASP Core Apps

    As I was looking about with some of the Core 2.2 notes, I came across some interesting notes on how to tweak about with Kestrel settings….

    I’m not sure I like putting Kestrel options up the in the Main Program builder. I tend to prefer these to collect in the startup class, but I get point of why Kestrel needs to be handled close to the Builder (it is after all the html service handling web calls).

    Shall we begin…
    So up in program.cs we do our normal Web Host Builder:

    public class Program
    {
        public static void Main(string[] args)
        {
            var host = CreateWebHostBuilder(args).Build();
    
            MigrateDatabase(host);
    
            host.Run();
            //CreateWebHostBuilder(args).Build().Run();
        }
    
        private static void MigrateDatabase(IWebHost host)
        {
            using (var scope = host.Services.CreateScope())
            {
                var db = scope.ServiceProvider.GetRequiredService&lt;ApplicationDbContext>();
                db.Database.Migrate();
            }
        }
    
        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup&lt;Startup>();
    }

    This tends to be my way of doing Program.cs. Note I split out the Build part into a static method (the normal default way is to have one fluent state that does a build and run in single line- it’s that commented line in the Main method) 

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
                WebHost.CreateDefaultBuilder(args)
                    .UseStartup$lt;Startup>()
                    .ConfigureKestrel(opts =>
                    {
                        opts.Limits.MaxConcurrentConnections = 100;
    
                    });

     In my example, the method I push Kestrel setting to would of course have the .ConfigureKestrel with it’s options settings… 

    Check it out… lots of interesting tweaks on settings…

    You will find some details here on MS DOCS:

    Learn about Kestrel, the cross-platform web server for ASP.NET Core.

     

  • ASP.NET Core

    EF Migration tasks in the Program.cs – The good and the Ugly

    Program.cs of any core project (especially ASP.Net Core) is our first starting point to get some necessary work out of the wrong. In this case EF DB Migrations. You know that EF thing where you keep your attached DB tables in sync with your data models? (or in some cases… mess up your existing database).

    Life in program.cs normally starts here:

    public class Program
    {
        public static void Main(string[] args)
        {
            CreateWebHostBuilder(args).Build().Run();
        }
    
        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup&lt;Startup>();
    }

    But let’s consider this:

     

    public class Program
    {
        public static async Task Main(string[] args)
        {
            IWebHost webHost = CreateWebHostBuilder(args).Build();
    
            // Create a new scope
            using (var scope = webHost.Services.CreateScope())
            {
                // Get the DbContext instance
                var myDbContext = scope.ServiceProvider.GetRequiredService&lt;MyDbContext>();
    
                //Do the migration asynchronously
                await myDbContext.Database.MigrateAsync();
            }
    
            // Run the WebHost, and start accepting requests
            // There's an async overload, so we may as well use it
            await webHost.RunAsync();
        }
    
        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup&lt;Startup>();
    }

     

    What the? So what is going on here… Well, basically we are injecting our EF DB Migration process between the creating of our web app (Build) and the actual Running of the site… You know that period of time when processes are defined and setup and actually begin listening on the wire.

     Have a look at this article….

    https://andrewlock.net/running-async-tasks-on-app-startup-in-asp-net-core-part-1/