MeTitus
MeTitus

Reputation: 3428

Routes are not initialized

I have the following setup:

Solution structure

Which is pretty simple. Now the situation I am facing is quite strange. If I place the Startup file in the Test project I get a 404 for all the routes, if I move that file to the WebApplication1 all routes are found.

This is what the Startup class looks like:

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Swashbuckle.AspNetCore.Swagger;

namespace Test
{
    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)
        {
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new Info { Title = "My API", Version = "v1" });
            });
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            app.UseSwagger();

            // Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.), 
            // specifying the Swagger JSON endpoint.
            app.UseSwaggerUI(c =>
            {
                c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
            });

            app.UseMvc();
        }
    }
}

I can't give any more details because that's all I have. Moving the class from one project to the other create an issue and I can't see why.

The test project is just a class library:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netcoreapp2.1</TargetFramework>
    <CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore" Version="2.1.1" />
    <PackageReference Include="Microsoft.AspNetCore.App" Version="2.1.1" />
    <PackageReference Include="Swashbuckle.AspNetCore.Swagger" Version="3.0.0" />
    <PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="3.0.0" />
    <PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="3.0.0" />
  </ItemGroup>

</Project>

UPDATE

The question, just to make it clear, is why does moving the Startup class to the Test project stops the routes being initialized?

Thanks in advance.

UPDATE

The Test project is actually just a Common set of base classes for web project, sorry for the wrong name. So if I moved the Startup class from the Common.Web project to WebApplication1 and update the namespaces all works fine, doing the otherwise stops it from working. Debugging shows the Startup class being called which is even more odd.

Solution structure

Upvotes: 1

Views: 72

Answers (1)

MeTitus
MeTitus

Reputation: 3428

I got it working, this is what fixed it:

public virtual void ConfigureServices(IServiceCollection services)
{
    var mvcModule = services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

    foreach(var assembly in EndpointAssemblies)
    {
        mvcModule.AddApplicationPart(assembly);
    }

Where EndpointAssemblies is just a list of defined assemblies. mvcModule.AddControllersAsServices(); }

Upvotes: 2

Related Questions