ASP.Net 核心 RC1:System.ArgumentException:路径中存在非法字符
ASP.Net Core RC1: System.ArgumentException: Illegal characters in path
Please note: As the answer shows this problem is specific for ASP.Net Core Release Candidate 1.
在 ASP.Net Core / MVC 6 中访问 URL http://localhost:58000/Admin/RebuildIndex/AspNetUserRoles/PK_IdentityUserRole%3Cstring%3E
出现此错误:
System.ArgumentException: Illegal characters in path.
at System.IO.Path.CheckInvalidPathChars(String path, Boolean
checkAdditional)
at System.IO.Path.GetExtension(String path)
at Microsoft.AspNet.StaticFiles.FileExtensionContentTypeProvider.TryGetContentType(String subpath, String& contentType)
at Microsoft.AspNet.StaticFiles.StaticFileContext.LookupContentType()
at Microsoft.AspNet.StaticFiles.StaticFileMiddleware.Invoke(HttpContext
context)
at Microsoft.AspNet.IISPlatformHandler.IISPlatformHandlerMiddleware.d__8.MoveNext()
这是由于 URL 中的 < 和 > 字符造成的。如果我访问不同的页面,例如 http://localhost:58000/Admin/RebuildIndex/AspNetRoles/RoleNameIndex,那么它工作正常。
我的路线是这样的:
app.UseMvc(routes =>
{
// Area route
routes.MapRoute(name: "areaRoute",
template: "{area:exists}/{controller}/{action}",
defaults: new { controller = "Home", action = "Index" });
// Default route
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
我正在访问的控制器如下所示:
[Route("Admin")]
public class AdminController : Controller
{
[Route("RebuildIndex/{tableName}/{indexName}")]
[HttpGet]
[Authorize(Roles="Admin")]
public async Task<IActionResult> RebuildIndex(string tableName, string indexName)
{
// ...
}
}
在 ASP.Net 的早期版本中,我们在 Web.Config 中使用了 httpRuntime 标记来缓解 "A potentially dangerous Request.Path value was detected from the client (&)" 错误。所以我在 web.config 中尝试了以下(以及它的一些变体)但没有成功:
<system.web>
<httpRuntime requestValidationMode="2.0" relaxedUrlToFileSystemMapping="true" requestPathInvalidCharacters="" />
</system.web>
但是从错误消息来看,这个错误似乎是因为请求被作为静态文件处理,而不是被提供给 MVC 控制器。
如何在 ASP.Net Core 中解决这个问题?
如果有一个解决方案只允许特定路由使用它会很好。
这个问题已经为 RC2 记录并修复(根据 the roadmap,应该在 2016 年 2 月发布)。
- 检查 AspNet.StaticFiles 存储库的 issue 82。
如果您想知道,可以查看 FileExtensionContentTypeProvider
class 的最新版本。你会看到 TryGetExtension
不再使用 System.IO.Path.GetExtension
(他们甚至添加了一条你会发现很有趣的评论!):
public bool TryGetContentType(string subpath, out string contentType)
{
string extension = GetExtension(subpath);
if (extension == null)
{
contentType = null;
return false;
}
return Mappings.TryGetValue(extension, out contentType);
}
private static string GetExtension(string path)
{
// Don't use Path.GetExtension as that may throw an exception if there are
// invalid characters in the path. Invalid characters should be handled
// by the FileProviders
if (string.IsNullOrWhiteSpace(path))
{
return null;
}
int index = path.LastIndexOf('.');
if (index < 0)
{
return null;
}
return path.Substring(index);
}
所以我想您可以等待 RC2 发布或使用 StaticFiles 存储库的本地克隆。
Please note: As the answer shows this problem is specific for ASP.Net Core Release Candidate 1.
在 ASP.Net Core / MVC 6 中访问 URL http://localhost:58000/Admin/RebuildIndex/AspNetUserRoles/PK_IdentityUserRole%3Cstring%3E
出现此错误:
System.ArgumentException: Illegal characters in path.
at System.IO.Path.CheckInvalidPathChars(String path, Boolean checkAdditional)
at System.IO.Path.GetExtension(String path)
at Microsoft.AspNet.StaticFiles.FileExtensionContentTypeProvider.TryGetContentType(String subpath, String& contentType)
at Microsoft.AspNet.StaticFiles.StaticFileContext.LookupContentType()
at Microsoft.AspNet.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context)
at Microsoft.AspNet.IISPlatformHandler.IISPlatformHandlerMiddleware.d__8.MoveNext()
这是由于 URL 中的 < 和 > 字符造成的。如果我访问不同的页面,例如 http://localhost:58000/Admin/RebuildIndex/AspNetRoles/RoleNameIndex,那么它工作正常。
我的路线是这样的:
app.UseMvc(routes =>
{
// Area route
routes.MapRoute(name: "areaRoute",
template: "{area:exists}/{controller}/{action}",
defaults: new { controller = "Home", action = "Index" });
// Default route
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
我正在访问的控制器如下所示:
[Route("Admin")]
public class AdminController : Controller
{
[Route("RebuildIndex/{tableName}/{indexName}")]
[HttpGet]
[Authorize(Roles="Admin")]
public async Task<IActionResult> RebuildIndex(string tableName, string indexName)
{
// ...
}
}
在 ASP.Net 的早期版本中,我们在 Web.Config 中使用了 httpRuntime 标记来缓解 "A potentially dangerous Request.Path value was detected from the client (&)" 错误。所以我在 web.config 中尝试了以下(以及它的一些变体)但没有成功:
<system.web>
<httpRuntime requestValidationMode="2.0" relaxedUrlToFileSystemMapping="true" requestPathInvalidCharacters="" />
</system.web>
但是从错误消息来看,这个错误似乎是因为请求被作为静态文件处理,而不是被提供给 MVC 控制器。
如何在 ASP.Net Core 中解决这个问题?
如果有一个解决方案只允许特定路由使用它会很好。
这个问题已经为 RC2 记录并修复(根据 the roadmap,应该在 2016 年 2 月发布)。
- 检查 AspNet.StaticFiles 存储库的 issue 82。
如果您想知道,可以查看 FileExtensionContentTypeProvider
class 的最新版本。你会看到 TryGetExtension
不再使用 System.IO.Path.GetExtension
(他们甚至添加了一条你会发现很有趣的评论!):
public bool TryGetContentType(string subpath, out string contentType)
{
string extension = GetExtension(subpath);
if (extension == null)
{
contentType = null;
return false;
}
return Mappings.TryGetValue(extension, out contentType);
}
private static string GetExtension(string path)
{
// Don't use Path.GetExtension as that may throw an exception if there are
// invalid characters in the path. Invalid characters should be handled
// by the FileProviders
if (string.IsNullOrWhiteSpace(path))
{
return null;
}
int index = path.LastIndexOf('.');
if (index < 0)
{
return null;
}
return path.Substring(index);
}
所以我想您可以等待 RC2 发布或使用 StaticFiles 存储库的本地克隆。