【问题标题】:Asp.Net core 3.0 The path in 'value' must start with '/'Asp.Net core 3.0 'value'中的路径必须以'/'开头
【发布时间】:2020-02-15 12:44:08
【问题描述】:

我找不到此问题的调用堆栈,看起来控制器一切正常,但“asp-href-include”有问题。

这里是个例外:

ArgumentException: The path in 'value' must start with '/'. (Parameter 'value')
Microsoft.AspNetCore.Http.PathString..ctor(string value)
Microsoft.AspNetCore.Hosting.StaticWebAssets.StaticWebAssetsFileProvider.StartsWithBasePath(string subpath, out PathString rest)
Microsoft.AspNetCore.Hosting.StaticWebAssets.StaticWebAssetsFileProvider.GetDirectoryContents(string subpath)
Microsoft.Extensions.FileProviders.Composite.CompositeDirectoryContents.EnsureDirectoriesAreInitialized()
Microsoft.Extensions.FileProviders.Composite.CompositeDirectoryContents.EnsureFilesAreInitialized()
Microsoft.Extensions.FileProviders.Composite.CompositeDirectoryContents.GetEnumerator()
Microsoft.AspNetCore.Mvc.TagHelpers.FileProviderGlobbingDirectory.EnumerateFileSystemInfos()+MoveNext()
System.Collections.Generic.List<T>.InsertRange(int index, IEnumerable<T> collection)
System.Collections.Generic.List<T>.AddRange(IEnumerable<T> collection)
Microsoft.Extensions.FileSystemGlobbing.Internal.MatcherContext.Match(DirectoryInfoBase directory, string parentRelativePath)
Microsoft.Extensions.FileSystemGlobbing.Internal.MatcherContext.Match(DirectoryInfoBase directory, string parentRelativePath)
Microsoft.Extensions.FileSystemGlobbing.Internal.MatcherContext.Execute()
Microsoft.Extensions.FileSystemGlobbing.Matcher.Execute(DirectoryInfoBase directoryInfo)
Microsoft.AspNetCore.Mvc.TagHelpers.GlobbingUrlBuilder.FindFiles(Matcher matcher)
Microsoft.AspNetCore.Mvc.TagHelpers.GlobbingUrlBuilder.ExpandGlobbedUrl(string include, string exclude)
Microsoft.AspNetCore.Mvc.TagHelpers.GlobbingUrlBuilder.BuildUrlList(string staticUrl, string includePattern, string excludePattern)
Microsoft.AspNetCore.Mvc.TagHelpers.LinkTagHelper.BuildGlobbedLinkTags(TagHelperAttributeList attributes, TagHelperContent builder)
Microsoft.AspNetCore.Mvc.TagHelpers.LinkTagHelper.Process(TagHelperContext context, TagHelperOutput output)
Microsoft.AspNetCore.Razor.TagHelpers.TagHelper.ProcessAsync(TagHelperContext context, TagHelperOutput output)
Microsoft.AspNetCore.Razor.Runtime.TagHelpers.TagHelperRunner.RunAsync(TagHelperExecutionContext executionContext)
AspNetCore.Views_Shared__Layout.<ExecuteAsync>b__17_0() in _Layout.cshtml
-
    <meta charset="utf-8" />
    <title>bizz - @ViewData["Title"]</title>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta content="width=device-width, initial-scale=1" name="viewport" />
    <meta content="" name="description" />
    <meta content="" name="author" />
    <link asp-href-include="~/bundle/metronic-mandatory.min.css" rel="stylesheet" asp-append-version="true" /> <-- this line is highlighted red
    <link asp-href-include="~/bizz/style/common.min.css" rel="stylesheet" asp-append-version="true" />
    @RenderSection("style", false)
    <script asp-src-include="~/bundle/metronic-mandatory.min.js" asp-append-version="true"></script>
    <script asp-src-include="~/bundle/common.min.js" asp-append-version="true"></script>
    <script asp-src-include="~/bundle/jquery-validation.min.js" asp-append-version="true"></script>
Microsoft.AspNetCore.Razor.Runtime.TagHelpers.TagHelperExecutionContext.SetOutputContentAsync()
AspNetCore.Views_Shared__Layout.ExecuteAsync()
Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderPageCoreAsync(IRazorPage page, ViewContext context)
Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderPageAsync(IRazorPage page, ViewContext context, bool invokeViewStarts)
Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderLayoutAsync(ViewContext context, ViewBufferTextWriter bodyWriter)
Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderAsync(ViewContext context)
Microsoft.AspNetCore.Mvc.ViewFeatures.ViewExecutor.ExecuteAsync(ViewContext viewContext, string contentType, Nullable<int> statusCode)
Microsoft.AspNetCore.Mvc.ViewFeatures.ViewExecutor.ExecuteAsync(ActionContext actionContext, IView view, ViewDataDictionary viewData, ITempDataDictionary tempData, string contentType, Nullable<int> statusCode)
Microsoft.AspNetCore.Mvc.ViewFeatures.ViewResultExecutor.ExecuteAsync(ActionContext context, ViewResult result)
Microsoft.AspNetCore.Mvc.ViewResult.ExecuteResultAsync(ActionContext context)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResultFilterAsync>g__Awaited|29_0<TFilter, TFilterAsync>(ResourceInvoker invoker, Task lastTask, State next, Scope scope, object state, bool isCompleted)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResultExecutedContextSealed context)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.ResultNext<TFilter, TFilterAsync>(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeResultFilters()
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResourceFilter>g__Awaited|24_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, object state, bool isCompleted)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeFilterPipelineAsync()
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Logged|17_1(ResourceInvoker invoker)
Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.ResponseCompression.ResponseCompressionMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Diagnostics.StatusCodePagesMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

代码:

<html>
<head>
    <meta charset="utf-8" />
    <title>bizz- @ViewData["Title"]</title>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta content="width=device-width, initial-scale=1" name="viewport" />
    <meta content="" name="description" />
    <meta content="" name="author" />
    <link asp-href-include="~/bundle/metronic-mandatory.min.css" rel="stylesheet" asp-append-version="true" />
    <link asp-href-include="~/bizz/style/common.min.css" rel="stylesheet" asp-append-version="true" />
    @RenderSection("style", false)

    <script asp-src-include="~/bundle/metronic-mandatory.min.js" asp-append-version="true"></script>
    <script asp-src-include="~/bundle/common.min.js" asp-append-version="true"></script>
    <script asp-src-include="~/bundle/jquery-validation.min.js" asp-append-version="true"></script>
    @RenderSection("script", false)
... other code

控制器(如果需要)

public ViewResult Index()
        {


            return View();
        }

编辑: 也许我忘了提到这个 html 在 _Layout.cshtml 里面。它与问题有什么关系吗?另外,无论我在里面写什么,它都会恢复到原来的样子。

EDIT2:我根据其中一个 cmets 编辑了我的代码。 错误仍然是相同的。 picture

【问题讨论】:

    标签: html css asp.net-mvc asp.net-core


    【解决方案1】:

    您需要使用 @Url.Content 解析内容 URL,例如:

    <script asp-src-include="@Url.Content("~/bundle/metronic-mandatory.min.js")" asp-append-version="true"></script>
    

    【讨论】:

    • 好的,我已经尝试过您的评论,我编辑了第一行的代码以检查它是否有效。但显然它只是消失了?这真的很难解释,所以我只是为它制作了一个视频。对不起手机视频:link
    • @JeremyGyovai-Rafa 您是否尝试过清理和重建。即使资源不存在,~ 也应该展开?
    • 已根据您的评论编辑问题。即使使用编辑过的代码,问题仍然存在。
    【解决方案2】:

    我遇到了同样的问题,我认为这是 .NET Core 3 的问题。在此处检查 github 问题 - 在 3.0 中,当在 Scripts 部分中使用 asp-src-includes 标签助手来包含 javascript 文件时,会生成 500 错误。这个标签助手在 2.2 中工作得很好。

    Tag Helper asp-src-include Not Functional in Core 3.0 #14642

    【讨论】:

    • 感谢您的提醒。知道什么时候能解决这个问题吗?
    • GitHub 问题线程说 Preview 3,它将在 12 月的第一周左右锁定代码,所以不要指望修复至少要到 2019 年 12 月中旬才能发布,我会想。这是里程碑 - github.com/aspnet/AspNetCore/milestones
    猜你喜欢
    • 2017-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-03
    • 2016-04-05
    • 2017-03-19
    • 1970-01-01
    相关资源
    最近更新 更多