简体   繁体   English

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

[英]What is the correct order in Configuring ASP.NET Core application?

I've got an ASP.NET Core application with several Web Api controllers. 我有一个带有几个Web Api控制器的ASP.NET Core应用程序。 I can't tell if there is some connection, but the application was created with VS2015 update 2, and now I'm working with VS2015 update 3. So I created another Web Api controller, and when there is a query to that controller I'm having this exception: 我不知道是否有一些连接,但应用程序是使用VS2015更新2创建的,现在我正在使用VS2015更新3.所以我创建了另一个Web Api控制器,当有对该控制器的查询时有这个例外:

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()

The funny thing is that somehow StaticFileMiddleware is involved in processing a simple request to controller. 有趣的是,StaticFileMiddleware以某种方式参与处理对控制器的简单请求。 While I can fix the problem by changing the order of configuring the application by calling 虽然我可以通过调用更改配置应用程序的顺序来解决问题

app.UseMvc(...

before 之前

app.UseStaticFiles()

I still want to know how this happened and what's the correct order of configuring the application. 我仍然想知道这是如何发生的以及配置应用程序的正确顺序。

Keep in mind that all previously added controllers are working just fine with both ways of configuring, but the new one can work only with the latter. 请记住,所有以前添加的控制器在两种配置方式下都能正常工作,但新的控制器只能用于后者。

The new controller do not operate with static files. 新控制器不使用静态文件。

EDITED Routing: In controller: EDITED路由:在控制器中:

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

In Startup.cs: 在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?}");
        });

The troubled request: 陷入困境的要求:

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

If you use Core2 preview mixed with release nugets, you have to add the static file nuget to your project. 如果将Core2预览与发行版本一起使用, 则必须将静态文件nuget添加到项目中。 (Core2 already include it but you still have to add it to solve some issues) (Core2已经包含它但你仍然需要添加它来解决一些问题)

Microsoft.AspNetCore.StaticFiles Microsoft.AspNetCore.StaticFiles

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 ASP.NET Core 上配置 DefaultScheme 和 DefaultChallengeScheme 有什么意义? - What is the point of configuring DefaultScheme and DefaultChallengeScheme on ASP.NET Core? 为ASP.NET Core 2.02配置Nlog - Configuring Nlog for asp.net core 2.02 在ASP.NET Core 2中发出配置选项的问题 - Issue configuring options in ASP.NET Core 2 在 ASP.NET 核心标识中配置选项 - Configuring options in ASP.NET Core Identity ASP.NET Core 3.1 - WebApplicationFactory - WebApplicationFactoryContentRootAttribute 的正确用法是什么? - ASP.NET Core 3.1 - WebApplicationFactory - What is the correct usage for WebApplicationFactoryContentRootAttribute? ASP.NET Core 应用程序的 Configure 方法中是否有强制的语句顺序? - Is there a mandatory order of statements in the Configure method of a ASP.NET Core application? 为 ASP.NET 核心和 .NET 核心 CLT 配置服务 - Configuring services for ASP.NET core and .NET core CLT ASP.NET核心应用中最好的服务层结构是什么 - What is the best service layer structure in ASP.NET Core application ASP.NET Core 2.0使用Services.Configure配置身份验证 - ASP.NET Core 2.0 Configuring Authentication with Services.Configure 使用身份在 Asp.Net Core 3.1 中配置 cookie - Configuring cookies in Asp.Net Core 3.1 with Identity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM