Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net core ASP.Net核心RC1:System.ArgumentException:路径中的非法字符_Asp.net Core_Asp.net Core Mvc - Fatal编程技术网

Asp.net core ASP.Net核心RC1:System.ArgumentException:路径中的非法字符

Asp.net core ASP.Net核心RC1:System.ArgumentException:路径中的非法字符,asp.net-core,asp.net-core-mvc,Asp.net Core,Asp.net Core Mvc,请注意:正如答案所示,此问题是针对ASP.Net核心版本候选1的。 在ASP.NETCore/MVC6中访问URL 给出此错误: System.ArgumentException:路径中存在非法字符。 在System.IO.Path.CheckInvalidPathChars处(字符串路径,布尔值 选中附加项) 在System.IO.Path.GetExtension(字符串路径) 在Microsoft.AspNet.StaticFiles.FileExtensionContentTypePro

请注意:正如答案所示,此问题是针对ASP.Net核心版本候选1的。

在ASP.NETCore/MVC6中访问URL

给出此错误:

System.ArgumentException:路径中存在非法字符。
在System.IO.Path.CheckInvalidPathChars处(字符串路径,布尔值 选中附加项)
在System.IO.Path.GetExtension(字符串路径)
在Microsoft.AspNet.StaticFiles.FileExtensionContentTypeProvider.TryGetContentType(字符串子路径、字符串和内容类型)
在Microsoft.AspNet.StaticFiles.StaticFileContext.LookupContentType()中 在Microsoft.AspNet.StaticFiles.StaticFileMiddleware.Invoke(HttpContext)中 (上下文)
在Microsoft.AspNet.IISPlatformHandler.IISPlatformHandlerMiddleware.d_u8.MoveNext()

这是由于URL中的<和>字符造成的。如果我访问不同的页面,例如,那么它工作正常

我的路线是:

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)
    {
        // ...
    }
}
[路由(“管理”)]
公共类AdminController:Controller
{
[路由(“重建索引/{tableName}/{indexName}”)]
[HttpGet]
[授权(Roles=“Admin”)]
公共异步任务重建索引(字符串表名、字符串索引名)
{
// ...
}
}
在ASP.Net的早期版本中,当我们收到“从客户端(&)检测到潜在危险的请求。路径值”错误时,我们使用Web.Config中的标记来放松。因此,我在web.config中尝试了以下内容(以及它的一些变体),但没有成功:

  <system.web>
    <httpRuntime requestValidationMode="2.0" relaxedUrlToFileSystemMapping="true" requestPathInvalidCharacters="" />
  </system.web>

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


如何在ASP.Net Core中解决这一问题?
如果有一个只允许特定路线使用的解决方案,那就太好了。

这是一个已记录的问题,并已针对RC2进行了修复(应于2016年2月发布,根据)

  • 检查AspNet.StaticFiles repo的
如果您想知道,您可以查看该类的最新版本。您将看到
TryGetExtension
不再使用
System.IO.Path.GetExtension
(他们甚至添加了一条注释,您会发现这很有趣!)

public bool TryGetContentType(字符串子路径,输出字符串contentType)
{
字符串扩展名=GetExtension(子路径);
if(扩展==null)
{
contentType=null;
返回false;
}
返回映射.TryGetValue(扩展名,out contentType);
}
私有静态字符串GetExtension(字符串路径)
{
//不要使用Path.GetExtension,因为如果存在异常,可能会引发异常
//路径中的字符无效。应处理无效字符
//由文件提供者提供
if(string.IsNullOrWhiteSpace(path))
{
返回null;
}
int index=path.LastIndexOf('.');
如果(指数<0)
{
返回null;
}
返回路径.子字符串(索引);
}
因此,我想您可以等待RC2发布,也可以使用您的本地回购克隆。

谢谢。在标题中添加了“RC1”,因为本问答是针对RC1的。我等RC2。
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);
}