Request Dev Services ยป

theDevTip.com

.NET Core Microsite Demo w/ CRUD functionality + 404

I have largely been opposed to pure Microsoft Stack Technologies in the past such as ASP.NET. Luckily with the more cross platform friendly ‘.NET Core‘, I can more easily get around the previous notions of the .NET technologies and frameworks; and enjoy some of it’s fruits, such as speed. It’s a less heavy development environment then it’s predecessor, and can be used to develop on OS-X or Linux. In this article I’ll illustrate building a simple micro-site out with .NET Core and C# using Visual Studio 7.4.3 on O-SX. Because, why not?


My video demo of putting together a simple .NET Core and C# Microsite with some basic functionality and error handling:



Key C# source codes

Within ‘code behind file‘ for CRUD tool page; notice the below C# code powering the functionalities of Creating, and Deleting Users.

namespace DevExpress.Xpo.Demo.Pages
{
    public class DataModel : PageModel {
        readonly UnitOfWork uow;
        public DataModel(UnitOfWork uow) {
            this.uow = uow;
        }

        public List<UserModel> Users { get; set; }
        public int TotalCount { get; set; }
        public void OnGet() {
            Users = uow.Query<User>()
                .OrderBy(u => u.LastName)
                .ThenBy(u => u.FirstName)
                .Select(u => new UserModel {
                    Oid = u.Oid,
                    FirstName = u.FirstName,
                    LastName = u.LastName,
                    Email = u.Email
                }).ToList();
            TotalCount = uow.Query<User>().Count();
        }
        [HttpPost]
        public IActionResult OnPostDelete(Guid id) {
            var user = uow.GetObjectByKey<User>(id);
            if(user != null) {
                uow.Delete(user);
                uow.CommitChanges();
            }
            return RedirectToPage();
        }
        [HttpPost]
        public IActionResult OnPostCreate(UserModel model) {
            if(model != null) {
                var newUser = new User(uow) {
                    FirstName = model.FirstName,
                    LastName = model.LastName,
                    Email = model.Email
                };
                uow.CommitChanges();
            }
            return RedirectToPage();
        }
    }
    public class UserModel {
        public Guid Oid { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Email { get; set; }
    }
}

in Startup.cs, notice the below code for 404 redirects and error handling

        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            app.UseXpoDemoData();

            app.UseStatusCodePages();
            app.UseStaticFiles();

            app.Use(async (context, next) => {
                await next();
                if (context.Response.StatusCode == 404)
                {
                    context.Request.Path = "/404";
                    await next();
                }
            });

            app.UseMvcWithDefaultRoute();

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseBrowserLink();
            }
            else
            {
                app.UseExceptionHandler("/Error");
            }

            app.UseStaticFiles();

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller}/{action=Index}/{id?}");
            });
        }

Sweet! Some benefits of .NET Core

“Flexible deployment: Can be included in your app or installed side-by-side user- or machine-wide. Cross-platform: Runs on Windows, macOS and Linux; can be ported to other OSes. The supported Operating Systems (OS), CPUs and application scenarios will grow over time, provided by Microsoft, other companies, and individuals. Command-line tools: All product scenarios can be exercised at the command-line. Compatible: .NET Core is compatible with .NET Framework, Xamarin and Mono, via the .NET Standard Library. Open source: The .NET Core platform is open source, using MIT and Apache 2 licenses. Documentation is licensed under CC-BY. .NET Core is a .NET Foundation project. Supported by Microsoft: .NET Core is supported by Microsoft, per .NET Core Support” – ‘What is .NET Core’ at https://stackoverflow.com/questions/26908049/what-is-net-core

About the Author
Cameron Cashwell Web Developer
I build websites, web apps, and software. Wanna work together? Let's chat about your project!

Leave a Reply

avatar
  Subscribe  
Notify of