ASP.Net Core - Request reached the end of the middleware pipeline
I was porting an MVC project from .Net Framework to .Net 8.
After the first pass, I had this runtime error:
Request reached the end of the middleware pipeline without being handled by application code. Request path: GET https://localhost:57926/, Response status code: 404
…and none of my controllers would handle incoming requests.
Quick fix
Somewhere on the web, I got the idea to add this snippet to Program.cs
:
builder.Services
.PartManager.ApplicationParts.Add(new Microsoft.AspNetCore.Mvc.ApplicationParts.AssemblyPart(Assembly.GetExecutingAssembly()));
…and this made it work! But I was not about to leave this ugly kludge in the codebase. I shouldn’t have to explicitly add this MVC site’s components to itself, because it is itself - it has one cohesive namespace with everything it needs included.
I then played some spot the difference between a brand new .Net Core MVC project and my project.
A few things I noticed and fixed
During the port, I’d changed the web project name from SqlSherlock
to SqlSherlock.Web
and should have known that VS is not clever enough to comprehensively rename everything, so I manually ensured all my Controllers matched this namespace.
That didn’t help.
In the csproj, I also removed the keys for RootNamespace and AssemblyName:
<!-- Deleted these: -->
<RootNamespace>SqlSherlock</RootNamespace>
<AssemblyName>SqlSherlock</AssemblyName>
So that they default to the (implicit) dynamic values, which are:
- Assembly name: $(MSBuildProjectName)
- Default namespace: $(MSBuildProjectName.Replace(“ “, “_”))
That didn’t help. But it was nice to be removing cruft, replacing hard config with convention.
I also checked my LaunchSettings, which still showed the old project name in the Debug bar. You can recreate launchsettings.json
by deleting it and reopening Visual Studio. Or you could copy from another working project and change the names. This didn’t help me either.
What did work
I had some package references in my web csproj, to old junk I didn’t need:
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.2.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="System.Configuration.ConfigurationManager" Version="7.0.0" />
</ItemGroup>
I deleted this whole section.
I am guessing it was the MVC one that really screwed me up.
In a modern csproj, the top line, <Project Sdk="Microsoft.NET.Sdk.Web">
means that MVC will be auto-included, so by including a PackageReference
, I was importing an older version that messed up the bindings.
ConfigurationManager is no longer needed, as we’re now using appsettings
instead of web.config
.
Newtonsoft.Json
has been replaced with System.Text.Json
.
When I removed the outdated package references, the controller started catching, and the site came to life, woop! ⏩🛺