[英]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.