gpt4 book ai didi

c# - 配置 ASP.NET Core 应用程序的正确顺序是什么?

转载 作者:太空狗 更新时间:2023-10-29 22:00:12 26 4
gpt4 key购买 nike

我有一个带有多个 Web Api Controller 的 ASP.NET Core 应用程序。我不知道是否有某种联系,但该应用程序是使用 VS2015 更新 2 创建的,现在我正在使用 VS2015 更新 3。所以我创建了另一个 Web Api Controller ,当对该 Controller 有查询时,我遇到了这个异常:

System.NotSupportedException: The given path's format is not supported.
at System.Security.Permissions.FileIOPermission.QuickDemand(FileIOPermissionAccess access, String fullPath, Boolean checkForDuplicates, Boolean needFullPath)
at System.IO.Path.GetFullPath(String path)
at Microsoft.AspNet.FileProviders.PhysicalFileProvider.GetFullPath(String path)
at Microsoft.AspNet.FileProviders.PhysicalFileProvider.GetFileInfo(String subpath)
at Microsoft.AspNet.StaticFiles.StaticFileContext.LookupFileInfo()
at Microsoft.AspNet.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context)
at Microsoft.AspNet.Cors.Infrastructure.CorsMiddleware.<Invoke>d__7.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
at Microsoft.AspNet.Diagnostics.Entity.MigrationsEndPointMiddleware.<Invoke>d__5.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
at Microsoft.AspNet.Diagnostics.Entity.DatabaseErrorPageMiddleware.<Invoke>d__6.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at Microsoft.AspNet.Diagnostics.Entity.DatabaseErrorPageMiddleware.<Invoke>d__6.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
at Microsoft.AspNet.Diagnostics.DeveloperExceptionPageMiddleware.<Invoke>d__7.MoveNext()

有趣的是,StaticFileMiddleware 以某种方式参与处理对 Controller 的简单请求。虽然我可以通过调用

更改配置应用程序的顺序来解决问题
app.UseMvc(...

之前

app.UseStaticFiles()

我仍然想知道这是怎么发生的以及配置应用程序的正确顺序是什么。

请记住,所有以前添加的 Controller 都可以在两种配置方式下正常工作,但新 Controller 只能与后者一起工作。

新 Controller 不使用静态文件进行操作。

已编辑路由:在 Controller 中:

[Route("api/[controller]")]
public class ViewsController : Controller
{
[HttpGet("{path}", Name = "Views")]
public async Task<IActionResult> Get(string path)
{
return Json("bla");
}
}

在 Startup.cs 中:

app.UseMvc(routes =>
{
routes.MapRoute(
name: "angular2app",
template: "ng/{*.}",
defaults: new { controller = "Home", action = "Index" });

routes.MapRoute(
name: "api",
template: "api/{controller}/{action}/{id?}");

routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});

麻烦的请求:

http://localhost:5000/api/Views/blablapath

最佳答案

如果您将 Core2 预览与发布 nuget 混合使用,您必须将静态文件 nuget 添加到您的项目中。 (Core2已经包含了,但还是要加上才能解决一些问题)

Microsoft.AspNetCore.StaticFiles

关于c# - 配置 ASP.NET Core 应用程序的正确顺序是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38654455/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com