ASP.Net Core RC1:System.ArgumentException:路径中的非法字符

Ted*_*sen 6 asp.net-core-mvc asp.net-core

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:路径中的非法字符.
   在System.IO.Path.CheckInvalidPathChars(字符串路径,布尔checkAdditional)
   在System.IO.Path.GetExtension(字符串路径)
   在Microsoft.AspNet.StaticFiles.FileExtensionContentTypeProvider.TryGetContentType(字符串子路径,字符串&的contentType)
   在Microsoft.AspNet.StaticFiles Microsoft.AspNet.IISPlatformHandler.IISPlatformHandlerMiddleware.d__8.MoveNext()
   上的Microsoft.AspNet.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context)
   中的.StaticFileContext.LookupContentType ()

这是由于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?}");
});
Run Code Online (Sandbox Code Playgroud)

我访问的控制器看起来像这样:

[Route("Admin")]
public class AdminController : Controller
{
    [Route("RebuildIndex/{tableName}/{indexName}")]
    [HttpGet]
    [Authorize(Roles="Admin")]
    public async Task<IActionResult> RebuildIndex(string tableName, string indexName)
    {
        // ...
    }
}
Run Code Online (Sandbox Code Playgroud)

在以前版本的ASP.Net中,我们使用Web.Config中的httpRuntime标记来放松当我们收到"从客户端(&)检测到潜在危险的Request.Path值"错误时.所以我在web.config中尝试了以下(以及它的一些变体)但没有成功:

  <system.web>
    <httpRuntime requestValidationMode="2.0" relaxedUrlToFileSystemMapping="true" requestPathInvalidCharacters="" />
  </system.web>
Run Code Online (Sandbox Code Playgroud)

但是,查看错误消息时,此错误似乎是因为请求作为静态文件处理而不是提供给MVC控制器.

如何在ASP.Net Core中解决这个问题?
如果只允许特定路线的解决方案,这将是一件好事.

Dan*_*.G. 1

这是RC2已记录并修复的问题(根据路线图,应于 2016 年 2 月发布)。

如果您想知道,您可以查看该FileExtensionContentTypeProvider课程的最新版本。你会看到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);
}
Run Code Online (Sandbox Code Playgroud)

所以我想您可以等待 RC2 发布,或者使用StaticFiles存储库的本地克隆。