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

时间:2016-07-29 08:49:44

标签: c# asp.net-web-api configuration asp.net-core static-files

我有一个带有几个Web Api控制器的ASP.NET核心应用程序。我无法判断是否存在某些连接,但应用程序是使用VS2015 update 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()

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

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

app.UseStaticFiles()

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

请记住,所有以前添加的控制器在两种配置方式下都能正常工作,但新的控制器只适用于后者。

新控制器无法使用静态文件。

EDITED 路由: 在控制器中:

[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

1 个答案:

答案 0 :(得分:0)

如果您将Core2 Preview与发行版nuget混合使用,则您必须将静态文件nuget添加到项目中。 (Core2已经包含了它,但是您仍然必须添加它来解决一些问题)

  

Microsoft.AspNetCore.StaticFiles