ASP.NET MVC Core应用程序不会实例化HomeController来处理默认路由

时间:2017-12-29 20:00:00

标签: c# asp.net-core asp.net-core-mvc autofac

我正在使用Autofac作为ASP.NET Core 1.1应用程序的依赖项解析器。我没有使用启动类,而是使用基类来配置包括Autofac容器在内的所有内容,然后允许派生类覆盖自定义配置的方法。

以下是基类初始化方法中主机的基本设置:

_server = new WebHostBuilder()
    .UseUrls(Settings.AspNet.Urls)
    .UseKestrel()
    .UseIISIntegration()
    .UseContentRoot(_directory)
    .ConfigureServices(services => ConfigureServicesInternal(services, containerBuilder))
    .Configure(app => ConfigureInternal(app, containerBuilder))
    .Build();
 _server.Run();

ConfigureServices()

正如您所看到的,没有涉及Startup类。这是什么基类'  ConfigureServicesInternal方法可以:

services.AddAutofac();
services.AddSingleton(Settings);

// Allow derived class to add services
ConfigureServices(services);

// Copy everything over to the Autofac container builder
builder.Populate(services);

// Allow derived class to register dependencies in the Autofac container
ConfigureContainer(builder);

这是派生类'ConfigureServices方法:

// Tried with and without AddControllersAsServices(), no difference
services.AddMvcCore().AddControllersAsServices();

派生类'ConfigureContainer方法为空。

Configure()

这是基类Configure方法:

// Allow derived class to configure the app
Configure(app);

// Storing a reference to the Autofac container in the base class because I need it later
Container = builder.Build();

// Let Autofac take over. I tried removing this, and it did not help
app.ApplicationServices = new AutofacServiceProvider(Container);

这是派生类'Configure方法:

// Also tried UseMvc(route => route.MapRoute(...)), no difference
app.UseMvcWithDefaultRoute();
浏览到应用程序的根目录,我得到了404.我可以将路由添加为服务以及静态文件,它们都可以工作(例如,我可以从这个应用程序获得200响应,而不是从MVC控制器获得)。

我尝试从Controller继承,但这也没有什么区别。我尝试从HomeController中删除所有构造函数注入,但仍然没有运气。

尝试使用IIS Express运行它,没有结果。

尝试直接浏览路线,例如http://localhost:8001/home/index/,但又没有运气。

除了以外没有记录:

  

[20:58:26 INF]请求启动HTTP / 1.1 GET http://localhost:8001/

     

[20:58:26 INF]要求以304.8803ms 404结束

如何调试此问题?

编辑:

问题在于将ConfigureInternal作为Configure方法传递,并从Configure内调用派生类“ConfigureInternal方法”。当我这样做时,它开始返回404默认路由。

当我传递派生类“Configure方法时,我可以看到在每次app.UseSomething()调用后添加到应用程序中的已解决服务。

我可以逐步完成这两种方法,并且我可以看到在派生类“Configure方法中每次使用调用后添加额外服务(例如app.UseRouter()添加路由选项和”路由标记服务“ ,app.UseMvcWithDefaultRouter()增加了另外19项服务。)

当我传递基类“ConfigureInternal方法并调用基类”Configure方法时,也会发生这种情况。所以我不明白为什么它会有所作为......

0 个答案:

没有答案
相关问题