加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-core – ASP.Net核心RC1:System.ArgumentException:路

发布时间:2020-12-16 03:45:45 所属栏目:asp.Net 来源:网络整理
导读:请注意:由于答案显示此问题特定于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
请注意:由于答案显示此问题特定于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标记来放松一些东西,当我们得到“从客户端(&)检测到一个潜在危险的Request.Path值”错误.所以我在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课程的最新版本.您将看到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 repo的本地克隆.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读