ASP .NET Core MVC, partitioned controllers and views into several csprojs

To partition your asp.net core project into several csprojs is easy or maybe you want to make use of one or more common functionality between multiple apps. ASP.Net Core came with an abstraction over the resource of an app, it’s called application parts. Using an appliction part, we can share controllers,views, tag helpers etc into more than one application. It can also be used as a way to structure your asp.net core project into specific projects where the project contains all resource from controllers to views.

How to

For example if you have an ASP .NET Core MVC called demoapplicationpart you can add a new csproj as part of the application and put it in a different projec. Here is the steps to do it:

  1. Create a separate class library csproj, give it name demoapplication.part1.csproj.
  2. Install the following nuget packages
    • Microsoft.AspNetCore.Mvc.Abstractions
    • Microsoft.AspNetCore.Mvc.ViewFeatures
  3. Open demoapplication.part1.csproj and add all views as an embedded resource.
  4. Go to Startup.cs in demoapplicationpart.csproj and get the assembly of demoapplication.part1 and add it as application part.
using demoapplicationpart.part1.Controllers;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Reflection;
using Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation;
using Microsoft.Extensions.FileProviders;
namespace demoapplicationpart.mvc
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
var assembly = typeof(Part1Controller).Assembly;
services.AddControllersWithViews()
.AddApplicationPart(assembly)
.AddRazorRuntimeCompilation();
services.Configure<MvcRazorRuntimeCompilationOptions>(options =>
{
options.FileProviders.Add(new EmbeddedFileProvider(assembly));
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
}
}
}
view raw Startup.cs hosted with ❤ by GitHub

after you done with the above steps, go ahead build and try running your application.

Source code

Published by Gadael Sedubun

Developer

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: