Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
357 views
in Technique[技术] by (71.8m points)

ASP.NET 5.0 find controllers on another assembly

My company will develop several services using ASP.NET 5. These services will provide both REST and gRPC APIs. Different teams will be developing them.

To increase developer productivity and to have a consistent HTTP pipeline we want to provide a NuGet package that will configure this HTTP pipeline (only the middlewares we want and in the correct order), configure logging (Serilog) and configuration (Azure App Configuration), and provide other helpers as well.

In order to do that I have created a library where we create the ASP.NET host with a standard startup class. Then I have created a sample ASP.NET 5 consuming this library via NuGet and started the application. The HTTP pipeline works correctly, but the controllers I have defined in the service assembly (not the library) are not found.

Does anyone know whats is the logic ASP.NET 5 uses to find the controllers? I did some research and tried some fixes, but none of them solved my issue.

Thanks for any help.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

We could share controllers, views, Razor Pages and more with Application Parts.

Application Parts allow ASP.NET Core to discover controllers, view components, tag helpers, Razor Pages, razor compilation sources, and more. AssemblyPart is an Application part. AssemblyPart encapsulates an assembly reference and exposes types and compilation references.

You could refer the following steps to use Application Parts.

  1. Create a ClassLibray (.net core) named "MySharedApp", and add the following package via NuGet.

    • Microsoft.Extensions.FileProviders.Embedded
    • Microsoft.AspNetCore.Mvc
  2. Add the Controller and Views with the following folder structure:

    enter image description here

    MySharedController.cs:

     //Require using Microsoft.AspNetCore.Mvc;
     public class MySharedController : Controller
     {
         public IActionResult Index()
         {
             return Ok("Message from shared assembly!");
         }
    
         public IActionResult IndexView()
         {
             return View("Index");
         }
     }
    

    Index.cshtml:

     <h1>
         Message from shared assembly!
     </h1>
    
     <h2> Using a View</h2>
    

    [Note] For the view pages, right click it and open the Properties panel, set the Build Action as "Embedded resource" (it can prevent the view page not found issue in the Main web app).

  3. Build the ClassLibrary.

  4. In the same solution, create a new Asp.net Core Web Application (MVC application).

  5. Right click the Dependencies, select "Add Project Reference...", select the MySharedApp ClassLibary, then click OK to add the reference.

    You could also click the Dependencies, select "Add Project Reference...", then, in the Browser tab, Click the "Browser" button and select the MySharedApp.dll, then click OK to add the reference.

    After that, the Dependencies looks as below:

    enter image description here

  6. In the MVC application Startup.ConfigureServices method, use the following code to discover and load the controller and view.

     public void ConfigureServices(IServiceCollection services)
     { 
         // services.AddControllersWithViews();
    
         #region
         // Requires MySharedApp.Controllers;
         // Requires using Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation;
         // Requires using Microsoft.Extensions.FileProviders;
         var assembly = typeof(MySharedController).Assembly;
         services.AddControllersWithViews()
             .AddApplicationPart(assembly)
             .AddRazorRuntimeCompilation();
    
         services.Configure<MvcRazorRuntimeCompilationOptions>(options =>
         { options.FileProviders.Add(new EmbeddedFileProvider(assembly)); });
    
         #endregion 
     }
    
  7. After that, add the hyperlink to navigate the controller and view:

     <li class="nav-item">
         <a class="nav-link text-dark" asp-area="" asp-controller="MyShared" asp-action="Index">MyShared</a>
     </li>
     <li class="nav-item">
         <a class="nav-link text-dark" asp-area="" asp-controller="MyShared" asp-action="IndexView">MyShared View</a>
     </li>
    

The test screenshot as below:

enter image description here

More detail information about using Application Parts, check Share controllers, views, Razor Pages and more with Application Parts


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...