Contoso University updated to ASP.NET Core

I pushed out a new repository, Contoso University Core, that updated my “how we do MVC” sample app to ASP.NET Core. It’s still on full .NET framework, but I also plan to push out a .NET Core version as well. In this, you can find usages of:

It uses all of the latest packages I’ve built out for the OSS I use, developed for ASP.NET Core applications. Here’s the Startup, for example:

public void ConfigureServices(IServiceCollection services)
{
    // Add framework services.
    services.AddMvc(opt =>
        {
            opt.Conventions.Add(new FeatureConvention());
            opt.Filters.Add(typeof(DbContextTransactionFilter));
            opt.Filters.Add(typeof(ValidatorActionFilter));
            opt.ModelBinderProviders.Insert(0, new EntityModelBinderProvider());
        })
        .AddRazorOptions(options =>
        {
            // {0} - Action Name
            // {1} - Controller Name
            // {2} - Area Name
            // {3} - Feature Name
            // Replace normal view location entirely
            options.ViewLocationFormats.Clear();
            options.ViewLocationFormats.Add("/Features/{3}/{1}/{0}.cshtml");
            options.ViewLocationFormats.Add("/Features/{3}/{0}.cshtml");
            options.ViewLocationFormats.Add("/Features/Shared/{0}.cshtml");
            options.ViewLocationExpanders.Add(new FeatureViewLocationExpander());
        })
        .AddFluentValidation(cfg => { cfg.RegisterValidatorsFromAssemblyContaining<Startup>(); });

    services.AddAutoMapper(typeof(Startup));
    services.AddMediatR(typeof(Startup));
    services.AddScoped(_ => new SchoolContext(Configuration["Data:DefaultConnection:ConnectionString"]));
    services.AddHtmlTags(new TagConventions());
}

Still missing are unit/integration tests, that’s next. Enjoy!

About Jimmy Bogard

I'm a technical architect with Headspring in Austin, TX. I focus on DDD, distributed systems, and any other acronym-centric design/architecture/methodology. I created AutoMapper and am a co-author of the ASP.NET MVC in Action books.
This entry was posted in ASPNetCore. Bookmark the permalink. Follow any comments here with the RSS feed for this post.
  • aka

    I just created an issue on Contoso University.
    Contoso University core seems to have the same problem.
    https://github.com/jbogard/ContosoUniversity/issues/31

  • Agile Hobo

    I have problem with creating a resource. In your implementation, after creating a resource (example: a department), you redirect to Index to display list of department. This is fine for MVC application but how should I implement it in Web API application as using command doesn’t return data but the API should return CreatedResult? Thanks.

    • jbogard

      You can return the URL of the created resource as part of your response, that’s kinda standard with the HTTP protocol.

      • Agile Hobo

        To produce the URL of the created resource I need to return id of created resource (or some unique information). But as I understand about CQRS, a command (to create resource) should not return any thing (the id or some unique information – in this case). Do I misunderstand about CQRS? Thanks.

        • jbogard

          No, that’s more CQS. CQRS doesn’t mean I don’t return anything, ever. It’s more that queries and commands are separate.

  • kiquenet kiquenet

    Contoso University sampe in Full Framework, and sample in ASPNET Core ?

    • jbogard

      Both.