【发布时间】:2022-03-01 00:31:23
【问题描述】:
我是 .Net Core MVC 的新手。我正在尝试将控制器添加到现有项目中,但出现错误
dbug: Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware[4]
The request path https://localhost:5001/api/admin/... does not match a supported file type
这让我觉得它甚至没有进入应用程序本身,而是在某处的某种中间件中被阻止。或者,为什么它抱怨文件类型?这里没有涉及“类型”,它只是一个包含 URL 的字符串。
在 VSCode 中,我将新控制器添加到与现有控制器相同的文件夹中,并且构建工作正常。 Ctrl-F5 在我的浏览器中打开它,我可以运行旧的 API(至少是我尝试过的 API),但不能运行新的;如果我使用新控制器的 URL,它会给出 404。
这也安装了 Swagger,Swagger 显示的是旧的 API 而不是新的。
在 Startup.cs 中有
app.UseRouting();
还有
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();
});
这是 Windows 10 上的 .Net Core 3.1.302 和 VSCode 1.48.0。
我从阅读教程中想到,添加控制器所需要做的就是编写代码(在现有的工作控制器上紧密建模)并构建它。但是必须有一些额外的步骤来在某处注册或记录新控制器?
这是正确的配置方法吗?来自 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;
}
【问题讨论】:
-
StaticFileMiddleware认为它是一个文件,这很奇怪。我们需要查看整个Configure方法以了解发生了什么。 -
你的问题做得很好,新的贡献者。我以前从未遇到过这个错误,但你看到this 的帖子了吗?
-
@Camilo Terevinto,我在哪里可以找到 Configure 方法?
-
@itsme86,我刚刚在看那个。我没有尝试过 ServeUnknownFileTypes = true,但我的问题是,如果这是问题,为什么旧的 API 可以工作?这就是这里看起来很神秘的地方——旧的可以工作,但是从旧的复制过来的新的只是名称从 UserController 更改为 PatientListController,不起作用。我认为它们要么都是未知类型,要么都可以工作。
-
@itsme86,感谢您对我的问题的称赞。我认为我经常因为没有就此类问题提供足够的信息而感到内疚,因此我试图提供可能相关的细节。
标签: c# asp.net-core-mvc