ASP.NET Core

ASP.Net Core. The go forward platform for creating the next generation of Web Apps.

  • .Net,  .Net Core,  ASP.NET Core,  Microsoft,  Visual Studio 2019

    Dot Net 5 is Here… Check it out.

    Wow… some most excellent videos on what is what in .Net 5! Check it out on

    [visual-link-preview encoded=”eyJ0eXBlIjoiZXh0ZXJuYWwiLCJwb3N0IjowLCJwb3N0X2xhYmVsIjoiIiwidXJsIjoiaHR0cHM6Ly93d3cuZG90bmV0Y29uZi5uZXQvIiwiaW1hZ2VfaWQiOjAsImltYWdlX3VybCI6IiIsInRpdGxlIjoiLk5FVCBDb25mIDIwMjAiLCJzdW1tYXJ5IjoiSm9pbiB0aGUgLk5FVCBDb25mIDIwMjAgZnJlZSB2aXJ0dWFsIGV2ZW50IE5vdmVtYmVyIDEwLTEyIHRvIGxlYXJuIGFib3V0IHRoZSBuZXdlc3QgZGV2ZWxvcG1lbnRzIGFjcm9zcyB0aGUgLk5FVCBwbGF0Zm9ybSwgb3BlbiBzb3VyY2UsIGFuZCBkZXYgdG9vbHMuIE1hcmsgeW91ciBjYWxlbmRhciEiLCJ0ZW1wbGF0ZSI6InVzZV9kZWZhdWx0X2Zyb21fc2V0dGluZ3MifQ==”]

    You can also find these on You Tube… search on .Net 5. The Conf 2020 Days 1-3 will be there…

    ~ScottGeek.

  • ASP.NET Core,  Blazor,  Microsoft

    The Server Side Blazor and SignalR Timeout

    So in my Blazor travels I ran across one (or another) annoying thing. In this one we will talk about that Server -Timeout? Disconnect? Retry? Not really sure which one it is.
    This affects Blazor Server Side where SinalR is being used between the browser and server side code.image

     

     

    I came across some Javescript that can go into the _Host.cshtml file.

    <script>
        // Wait until a 'reload' button appears
        new MutationObserver((mutations, observer) => {
            if (document.querySelector('#components-reconnect-modal h5 a')) {
                // Now every 10 seconds, see if the server appears to be back, and if so, reload
                async function attemptReload() {
                    await fetch(''); // Check the server really is back
                    location.reload();
                }
                observer.disconnect();
                attemptReload();
                setInterval(attemptReload, 10000);
            }
        }).observe(document.body, { childList: true, subtree: true });
    </script>

    Not really a solution but let’s see. The script is basic and checks the connection and reloads the page as needed.

    There’s a full run down here ASP Core Issues. Another good resource is this docs page ASP.NET Core SignalR configuration.

    Happy Blazor-ing!

    ~ScottGeek

     

  • 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

     

     

  • 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/

     

     

      

  • Article,  ASP.NET Core,  Microsoft

    ASP .NET Core – the new stuff

    The latest read concerning ASP.Net Core 2.1

    [visual-link-preview encoded=”eyJ0eXBlIjoiZXh0ZXJuYWwiLCJwb3N0IjowLCJwb3N0X2xhYmVsIjoiIiwidXJsIjoiaHR0cHM6Ly9tc2RuLm1pY3Jvc29mdC5jb20vZW4tdXMvbWFnYXppbmUvbXQ4Mjk3MDYiLCJpbWFnZV9pZCI6Njg3LCJpbWFnZV91cmwiOiJodHRwczovL3Njb3R0Z2Vlay50ZWNobm9sb2d5L3dwLWNvbnRlbnQvdXBsb2Fkcy8yMDE4LzA4L0lDODc5ODU5LmpwZyIsInRpdGxlIjoiQVNQLk5FVCBDb3JlIC0gV2hhdCdzIE5ldyBpbiBBU1AuTkVUIENvcmUgMi4xIiwic3VtbWFyeSI6Ik1pY3Jvc29mdCByZWNlbnRseSByZWxlYXNlZCBBU1AuTkVUIENvcmUgMi4xIGFsb25nIHdpdGggLk5FVCBDb3JlIDIuMSBhbmQgRW50aXR5IEZyYW1ld29yayAoRUYpIENvcmUgMi4xLiBDb21iaW5lZCwgdGhlc2UgcmVsZWFzZXMgb2ZmZXIgc29tZSBncmVhdCBpbXByb3ZlbW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