Reputation: 41
I'm new to .Net Core MVC. I'm trying to add a controller to an existing project, but I'm getting the error
dbug: Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware[4]
The request path https://localhost:5001/api/admin/... does not match a supported file type
This makes me think it's not even getting to the app itself but getting blocked in some kind of middleware somewhere. Or, why does it complain about file type? There's no "type" involved here, it's just a string containing a URL.
In VSCode I added the new controller in the same folder with existing controllers, and the build worked fine. Ctrl-F5 brings it up in my browser and I can run the old APIs (at least the ones that I tried) but not the new one; it gives a 404 if I use a URL to the new controller.
This also has Swagger installed, and Swagger shows the old APIs but not the new one.
In the Startup.cs it has
app.UseRouting();
and also
app.UseEndpoints(endpoints =>
{
endpoints.MapAreaControllerRoute(
name: "areas",
areaName: "areas",
pattern: "{area}/{controller=Home}/{action=Index}/{id?}");
endpoints.MapControllerRoute("default",
"{controller=Home}/{action=Index}/{id?}");
endpoints.MapRazorPages();
});
This is .Net Core 3.1.302 and VSCode 1.48.0 on Windows 10.
I had thought from reading tutorials that all I needed to do to add a controller was to write the code (modeled closely on an existing, working controller) and build it. But there must be some additional step to register or log the new controller somewhere?
Is this the right Configure method? From Startup.cs
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IServiceProvider services)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
CreateUserRoles(services).Wait();
}
else
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
#if Release
app.UseHttpsRedirection();
#endif
app.UseStaticFiles();
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetParent(Directory.GetCurrentDirectory()).FullName, "MobileApp.WebPortal", "wwwroot")),
ServeUnknownFileTypes = true
});
app.UseSession();
app.UseCookiePolicy();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseHangfireDashboard("/jobs", new DashboardOptions
{
Authorization = new [] { new HangfireAuthorizationFilter() },
AppPath = "/"
});
#if RELEASE
//app.UseWebMarkupMin();
#endif
app.UseEndpoints(endpoints =>
{
//endpoints.MapControllers();
//endpoints.MapRazorPages();
endpoints.MapAreaControllerRoute(
name: "areas",
areaName: "areas",
pattern: "{area}/{controller=Home}/{action=Index}/{id?}");
//endpoints.MapAreaControllerRoute(
// name: "internaldefault",
// areaName: "Internal",
// pattern: "Internal/{controller=Patient}/{action}/{id?}");
//endpoints.MapAreaControllerRoute(
// name: "identity",
// areaName: "Identity",
// pattern: "Identity/{controller=Account}/{action=Login}/{id?}");
endpoints.MapControllerRoute("default",
"{controller=Home}/{action=Index}/{id?}");
endpoints.MapRazorPages();
});
//var config = new MapperConfigurationExpression();
//config.AddProfile(new MapperProfile());
//Mapper.Initialize(config);
var options = new MemoryCacheEntryOptions() { SlidingExpiration = TimeSpan.FromHours(2)};
QueryCacheManager.DefaultMemoryCacheEntryOptions = options;
}
Upvotes: 4
Views: 4620
Reputation: 1
I had the exact same issue in VSCode. After 4 hours of going through websites I simply realised that when i added the new controller....I created the file in VSCode without the ".cs" extension!!!!! So my Controllers directory had 2 files called "AccountsController" and "ReportsController" and one "HomeController.cs"...and funnily enough only the home controller worked.
Upvotes: 0
Reputation: 41
Well, I have a partial answer to this that I don't entirely understand. I was working in VSCode. I created a new file for my PatientListController and put it in the appropriate place in the file hieracharcy, and got the error messages described above (plus the fact that it didn't show up in Swagger).
But if I work in Visual Studio (which I'm less familiar with), when I attempt to create a new file, it specifically asks me what I am creating and I choose Controller from the list.
In VSCode, apparently it doesn't know what this new file is and so the compiler just ignores it. So the compiled code has no trace of the new controller.
In Visual Studio the compiler knows that I've added a new controller and the exact same code works just fine and the new api shows up in Swagger.
So there must be some way in VSCode to signal what to do with the new file, but I have no idea how to make it do that. And I also don't understand why VSCode functions differently from Visual Studio in that respect.
Upvotes: 0