【问题标题】:ViewComponent tag helpers not workingViewComponent 标签助手不起作用
【发布时间】:2017-01-04 16:23:15
【问题描述】:

我已将我的 asp.net 核心 Web 应用程序从 1.0.1 更新到 1.1.0,但我的视图组件的标签助手不起作用:

<vc:login-form />

它输出标签。它使用旧语法:@await Component.InvokeAsync(typeof(LoginFormViewComponent))

我错过了什么?

package.json:

{
  "title": "DevPortal.Web",
  "version": "0.1.0",
  "language": "",
  "dependencies": {
    "Microsoft.NETCore.App": {
      "version": "1.1.0",
      "type": "platform"
    },
    "Microsoft.AspNetCore.StaticFiles": "1.1.0",
    "Microsoft.AspNetCore.Server.IISIntegration": "1.1.0",
    "Microsoft.AspNetCore.Server.Kestrel": "1.1.0",
    "Microsoft.AspNetCore.Mvc": "1.1.0",
    "NuGet.CommandLine": "3.5.0",
  },

  "tools": {
    "Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final",
    "Microsoft.AspNetCore.Razor.Tools": "1.1.0-preview4-final"
  },

  "frameworks": {
    "netcoreapp1.1": {
      "imports": [
        "dotnet5.6",
        "portable-net45+win8"
      ]
    }
  },

  "buildOptions": {
    "emitEntryPoint": true,
    "preserveCompilationContext": true
  },

  "runtimeOptions": {
    "configProperties": {
      "System.GC.Server": true
    }
  },

  "scripts": {
    "prepublish": [ "bower install"],
    "postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
  },
  "userSecretsId": "aspnet-DevPortal.Web-20161230052130"
}

我已将此添加到 _ViewImports.cshtml:

@addTagHelper "*, DevPortal"

我的程序集名称是 DevPortal.dll

[ViewComponent(Name ="LoginForm")]
public class LoginFormViewComponent: ViewComponent
{
    public IViewComponentResult Invoke(LoginFormViewModel model = null)
    {
        if (model == null) model = new LoginFormViewModel();
        return View(model);
    }
}

【问题讨论】:

  • 不确定您是否发现了发生这种情况的原因,但我遇到了同样的问题。我将跳转到 GitHub 存储库并报告问题,看看是否有人在任何地方都有这个中断列表。

标签: c# asp.net-core asp.net-core-viewcomponent asp.net-core-tag-helpers


【解决方案1】:

老问题,但有同样的问题。

根据this link,,似乎_ViewImports_GlobalImports 不会自动应用于ViewComponents。尝试在ViewComponent 视图中添加@addTagHelper 行。

【讨论】:

    【解决方案2】:

    在这种情况下,问题还在于具有默认值的参数。这个问题我们在github上跟踪,但是目前ViewComponents不能有默认值的参数

    这应该可行:

    <vc:login-form model="null" />    
    

    【讨论】:

    • 这个问题在 .NET Core 2.2 中仍然存在,好消息,这个答案仍然有效。
    【解决方案3】:

    我有一个 ASP.NET Core 5.0 Web 应用程序。我遇到了同样的问题,但我解决了这个问题。

    例如:

    如果我的项目名称是 MyProject 并且我的视图组件位于 Components 文件夹中,我将以下代码添加到 _ViewImport.cshtml 并且它对我有用

     @addTagHelper *, MyProject  
    

    更多详情可以看这个视频:Kudvenkat - ASP NET Core view component tag helper

    【讨论】:

    • 这对 .NET Core 5 非常有效。感谢分享。
    • 我错过了它是一个 AssemblyName,而不是当前程序集的命名空间。
    猜你喜欢
    • 2016-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-06
    相关资源
    最近更新 更多