question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ControllerBase Route attribute is ignored in dynamically loaded AssemblyParts

See original GitHub issue

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

The Route attribute of ControllerBase is ignored when an AssemblyPart is dynamically loaded (built using a separate C# project that uses nuget Microsoft.AspNetCore.Mvc.Core.dll).

I am loading a dynamic library (Assembly.Load) and then I add AsseblyParts to the IServiceCollection.AddControllers. Controller - in the other library (not referenced by the main Web project):

    [ApiController]
    [Route("api/[controller]")]
    [AllowAnonymous]
    public class AsopController : ControllerBase
    {
        public AsopController()
        {
        }

        [HttpGet()]
        public IActionResult Get()
        {
            return Ok(new { text = "OK" });
        }
    }

Startup.cs - ConfigureDevelopmentServices(IServiceCollection serviceCollection)

var pluginsPath = Path.Combine(AppContext.BaseDirectory, @".\Plugins");
string[] pluginPaths = Directory.GetFiles(pluginsPath, "*.Plugin.dll", SearchOption.AllDirectories);

            foreach (var pluginPath in pluginPaths)
            {
                Assembly pluginAssembly = pluginPath.LoadAssembly(); // this is just Assembly.Load

                IMvcBuilder mvcBuilder = serviceCollection.AddControllers();
                mvcBuilder.ConfigureApplicationPartManager(pm =>
                {
                    pm.ApplicationParts.Add(new AssemblyPart(pluginAssembly));
                });
            }

In ConfigureDevelopment(IApplicationBuilder app):

 app.UseRouting();
...
 app.UseEndpoints(endpoints =>
            {
                endpoints.MapDefaultControllerRoute();
            });

The endpoints are now populated with the MVC style routes: controller=Home/action=Index/… so, the initial route is ignored.

Expected Behavior

The request route should be found according to the definition in controller Route attribute.

Steps To Reproduce

See code above.

Exceptions (if any)

No response

.NET Version

6

Anything else?

No response

Issue Analytics

  • State:closed
  • Created 5 months ago
  • Comments:19 (12 by maintainers)

github_iconTop GitHub Comments

1reaction
eusebiucommented, Apr 30, 2023
0reactions
eusebiucommented, May 9, 2023

Then we can close the issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Webapi inherited controllers are ignoring Route attributes
I want to have a Controller Hierarchy assembly at webapi 2 website to handle common CRUD service operation. I Started with an abstract...
Read more >
Routing in ASP.NET Core
Routing is responsible for matching incoming HTTP requests and dispatching those requests to the app's executable endpoints.
Read more >
Dynamic Route in ASP.NET Core When ...
Dynamic route requirement means that we can't use [Route] attribute on the API service controller as it requires compilation time constant for ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found