Mvc: [2.1]: Mvc doesn't load controllers from referenced assemblies with "Microsoft.NET.Sdk.Web" sdk anymore

Created on 7 Jun 2018  路  13Comments  路  Source: aspnet/Mvc

After I migrated my project onto 2.1 from 2.0 it became broken - I started getting 404 for some routes.

Investigating revealed that controllers aren't loaded if they are in a referenced assembly with "Microsoft.NET.Sdk.Web" Sdk.

App (Sdk="Microsoft.NET.Sdk.Web")
Main/Startup

Lib (Sdk="Microsoft.NET.Sdk.Web")
Controller

Project "App" references project "Lib" and declare a route for controller that is in Lib project.

Both assemblies reference "Microsoft.AspNetCore.App".
Both projects have "Microsoft.NET.Sdk.Web" Sdk.
If Lib project will have "Microsoft.NET.Sdk" SDK then controllers will load.

More details:
Given a simple web-app. It has a route "" leads to HomeController. Run and see "Ok" in the broser:

    class Program
    {
        public static void Main(string[] args)
        {
            CreateWebHostBuilder(args).Build().Run();
        }
        public static IWebHostBuilder CreateWebHostBuilder(string[] args)
        {
            return WebHost.CreateDefaultBuilder(args).UseStartup<Startup>();
        }
    }

    public class Startup
    {
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();
        }

        public void Configure(IApplicationBuilder app)
        {
            app.UseMvc(routes => {
              routes.MapRoute("default", "", new {Controller = "Home",Action = "Index"});
            });            
        }
    }

    public class HomeController : Controller
    {
        [HttpGet]
        public string Index()
        {
            return "Ok";
        }
    }

Now add a new project "Class Library" (or "Console") and move HomeController into it and add reference to it into the main project. Run and get 404.

I guess that it's not a bug and probably it's by design, but this new behavior brakes things. It's not mentioned in the migration guide.

by design

All 13 comments

Would you mind setting up a repro app just so that we don't miss something? The scenario you listed should work (in fact I tried pretty similar to what you suggest with a F# class library earlier today - https://github.com/valainisgt/AddApplicationPart-FSharp).

One important addition - the issue takes place if and only if the referenced project has <Project Sdk="Microsoft.NET.Sdk.Web"> (with <Project Sdk="Microsoft.NET.Sdk"> it works). I'll fix initial description.

Also adding AddApplicationPart(<referenced assembly>) solves the problem. But in 2.0 it worked without this.

cc @JunTaoLuo - seems like this is running in to the same sort of issues that were seen in https://github.com/aspnet/Mvc/issues/7710. It appears that adding a reference to Microsoft.AspNetCore.All v2.1.0 or Microsoft.AspNetCore.App v2.1.0 requires referencing the Web Sdk to produce the correct deps file. Targeting a different Sdk results in the deps file missing any references to the AspNetCore shared runtime package and any transitive references. Summary of the past discussion here - https://github.com/aspnet/Mvc/issues/7710#issuecomment-386170059

I'll work with @danroth27 to get this + testing stuff covered in the 2.1 migration docs.

@evil-shrike if you do want to continue targeting Microsoft.NET.Sdk, the recommendation would be to replace the Microsoft.AspNetCore.App reference with references to individual packages e.g. Microsoft.AspNetCore.Mvc.

@evil-shrike sorry, I misread your issue. Let me dig in to this further.

@evil-shrike here's the set of rules around referencing a shared runtime package:

1) Project referencing Microsoft.AspNetCore.All or Microsoft.AspNetCore.App packages, must reference Microsoft.Net.Sdk.Web.
2) Projects referencing packages or projects that transitively reference Microsoft.AspNetCore.All or Microsoft.AspNetCore.App must reference Microsoft.Net.Sdk.Web and the same shared runtime package. E.g if LibraryA references Microsoft.AspNetCore.App, any projects referencing it must reference Microsoft.AspNetCore.App.
3) Executable projects, i.e. the app that you launch, must not specify the version. This includes "apps" that run and tests.
4) Referenced projects, i.e. projects that aren鈥檛 the entry point, that reference Microsoft.AspNetCore.All or Microsoft.AspNetCore.App must specify a package version.

In your case, referencing Microsoft.Net.Sdk.Web and not specifying a version in your library would be the correct way to go about this. We'll get this added to the migration notes.

Following

@pranavkm I guess the provided rules aren't quite complete as they didn't mention Microsoft.NET.Sdk.Razor sdk. As I understand it's equivalent to Microsoft.NET.Sdk.Web for library projects, right?

Right. However, if you're referencing Microsoft.NET.Sdk.Razor, we anticipate targeting netstandard2.0 \ Microsoft.AspNetCore.Mvc rather than one of the shared runtime packages. These problems generally crop up as part of referencing one of the two AspNetCore runtime packages.

I am battling with this issue. Some pages are routed to but others just fail.
This is a pain in my ass

@smithaitufe Are all pages in referenced assemblies? Are any of the pages in dynamically loaded assemblies?

Hi Everyone.

I'm struggling with this right now. Following the thread.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

grahamehorner picture grahamehorner  路  52Comments

NTaylorMullen picture NTaylorMullen  路  66Comments

janpieterz picture janpieterz  路  43Comments

pranavkm picture pranavkm  路  57Comments

MicahZoltu picture MicahZoltu  路  37Comments