我无法在我的ASP.NET v5项目中使用自定义TagHelper.自定义TagHelper位于Web项目本身.我尝试从CodeProject和本教程中获取此示例.
当我尝试不使用标记时addTagHelper:没有任何变换,标记帮助程序中没有任何断点.
当我尝试@addTagHelper "*, MyWebApplication"View组件时,错误是MyWebApplication'. Error: Could not load file or assembly 'MyWebApplication'.
我错过了什么?
工具版本:
Microsoft Visual Studio Enterprise 2015 RC
Version 14.0.22823.1 D14REL
Microsoft .NET Framework Version 4.6.00057
Microsoft.AspNet.Mvc 6.0.0-beta4
Run Code Online (Sandbox Code Playgroud) 在较旧的MVC HTML Helpers中,可以使用IDisposable包装内容 - 例如,BeginForm帮助程序将*stuff*使用结束form标记自动换行
<% using (Html.BeginForm()) {%>
*stuff*
<% } %>
Run Code Online (Sandbox Code Playgroud)
MVC6 TagHelpers是否支持这种内容包装?例如,我想这样
<widget-box title="My Title">Yay for content!</widget-box>
Run Code Online (Sandbox Code Playgroud)
要扩展为包装div的bootstrap小部件框:
<div class="widget-box">
<div class="widget-header">
<h4 class="widget-title">My Title</h4>
</div>
<div class="widget-body">
<div class="widget-main">
Yay for content!
</div>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
TagHelpers可以实现吗?
解决方案:我已经将@DanielJG的答案添加到github上的一个工作演示中,该演示使用了WidgetBoxTagHelper.cs(在我的生产应用程序中使用lib时将与Beta/RC/RTM保持同步)
我正在学习MVC 6.它没关系,但是在我的_ViewImports.cshtml中添加taghelper之后,我的应用停止了.
这些是我的依赖:
"dependencies": {
"Microsoft.AspNetCore.Mvc": "1.0.0-rc2-final",
"Microsoft.AspNetCore.Server.IISIntegration": "1.0.0-rc2-final",
"Microsoft.AspNetCore.Server.Kestrel": "1.0.0-rc2-final",
"Microsoft.AspNetCore.StaticFiles": "1.0.0-rc2-final",
"Microsoft.AspNet.Mvc.TagHelpers": "6.0.0-rc1-final",
"Microsoft.AspNetCore.Diagnostics": "1.0.0-rc2-final"
},
Run Code Online (Sandbox Code Playgroud)
我添加了这一行:
@addTagHelper "*, Microsoft.AspNet.Mvc.TagHelpers"
Run Code Online (Sandbox Code Playgroud)
现在我收到了错误:
无法解析包含程序集"Microsoft.AspNet.Mvc.TagHelpers"的TagHelper.错误:无法从程序集"Microsoft.Extensions.Logging.Abstractions,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = adb9793829ddae60"加载类型"Microsoft.Extensions.Logging.ILogValues".@addTagHelper"*,Microsoft.AspNet.Mvc.TagHelpers"
有什么建议 ?我找不到任何与同一消息有关的内容.
我正在ASP.NET Core(RC2)中开发标记帮助器,渲染标记帮助器时,我需要访问Request对象,因为我需要弄清楚请求的URL是什么。
因此,似乎在ASP.NET Core中访问Request对象的正确方法是从HttpContext,并获取HttpContextI,我需要将其注入IHttpContextAccessor到Tag Helper中。
所以我尝试了一下,但是在运行时抛出了以下异常:
InvalidOperationException:尝试激活“ Auth0.AspNetCore.Mvc.TagHelpers.LockTagHelper”时,无法解析类型为“ Microsoft.AspNetCore.Http.IHttpContextAccessor”的服务。
有什么原因我不能注入IHttpContextAccessor我的标签助手?
另外,也许还有另一种方法可以访问Tag Helper中的Request对象?
编辑
似乎问题在于,自最新的候选发布版以来,您必须手动配置DI才能处理IHttpContextAccessor。因此,ConfigureServices您必须致电
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
我相信在早期的Beta中,它是自动配置的...
问题是这是一个库,我不希望用户在其应用程序中配置该库以使我的库正常工作,因此仍然可以使用任何其他更可靠的方式来访问Request对象:)
CustomerEntryModel
[Required]
public String FirstName { get; set; }
Run Code Online (Sandbox Code Playgroud)
CustomerController.cs
[HttpGet]
[Route("Get")]
public IActionResult Get()
{
CustomerEntryModel model = new CustomerEntryModel();
return View("CustomerEntry", model);
}
[HttpPost]
[Route("Update")]
public ActionResult Update([FromForm]CustomerEntryModel model)
{
if (!ModelState.IsValid)
{
return View("CustomerEntry", model);
}
return null;
}
Run Code Online (Sandbox Code Playgroud)
CustomerEntry.cshtml
@addTagHelper "*, Microsoft.AspNet.Mvc.TagHelpers"
<form asp-controller="Customer" asp-action="Update" method="post">
<input type="text" asp-for="FirstName" />
<span asp-validation-for="FirstName" />
<input type="submit" value="Submit" />
</form>
Run Code Online (Sandbox Code Playgroud)
我没有包含jQuery验证库,因此验证发生在服务器端.现在,当我提交名为空的页面时,执行更新操作并收到相同的视图,但没有任何错误.
而不是<span asp-validation-for="FirstName" />标记助手,如果我使用html助手,@Html.ValidationMessageFor(m => m.FirstName)我得到名字所需的错误.
根据我的理解,TagHelper扩展了传统行为,HtmlHelper提供HTML友好的开发体验.这意味着,适用的东西必须HtmlHelper与它的TagHelper …
布局有这个:
<!DOCTYPE html>
<html>
<head>
<environment names="Development">@RenderSection("devCss", required: false)</environment>
<environment names="Staging,Production">@RenderSection("staproCss", required: false)</environment>
</head>
<body>
@RenderBody()
<environment names="Development">@RenderSection("devJs", required: false)</environment>
<environment names="Staging,Production">@RenderSection("staproJs", required: false)</environment>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
视图有这个:
@section devCss { <link rel="stylesheet" href="foo.css" asp-append-version="true" /> }
@section staproCss { <link rel="stylesheet" href="foo.min.css" asp-append-version="true" /> }
@section devJs {}
@section staproJs {}
<h1>hello</h1>
Run Code Online (Sandbox Code Playgroud)
当标签RenderSection()外时<environment>,一切正常。
在内部时,如上例所示,它失败并显示无用的错误 InvalidOperationException: The following sections have been defined but have not been rendered by the page at '_Layout.cshtml': 'staproCss, staproJs'. To ignore an …
我正在创建一个自定义HTML Tag Helper:
public class CustomTagHelper : TagHelper
{
[HtmlAttributeName("asp-for")]
public ModelExpression DataModel { get; set; }
public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
{
string content = RazorRenderingService.Render("TemplateName", DataModel.Model);
output.Content.SetContent(content);
}
}
Run Code Online (Sandbox Code Playgroud)
如何以编程方式呈现部分视图并在TagHelper.ProcessAsync中以字符串形式获取呈现的内容?
我应该请求注入IHtmlHelper吗?
是否有可能获得剃须刀引擎的参考?
我已经阅读了达米安·爱德华兹(Damian Edwards)的以下文章。
https://github.com/aspnet/Mvc/issues/1576
https://github.com/aspnet/Mvc/issues/1580
在时ScriptTagHelper,我不确定应该输入asp-fallback-test attribute什么?和一样jQuery吗?window.jQuery?如何找到正确的JavaScript表达式?有人可以给我一个例子moment-withlocales.min.js吗?谢谢!
以下示例LinkTagHelper正确吗?
<link
rel="stylesheet"
href="https://cdn.jsdelivr.net/bootstrap.daterangepicker/2/daterangepicker.css"
asp-fallback-href="~/lib/bootstrap-daterangepicker/daterangepicker.css"
asp-fallback-test-class="daterangepicker"
asp-fallback-test-property="position"
asp-fallback-test-value="absolute"
/>
Run Code Online (Sandbox Code Playgroud)
迈克尔
我写了一个标签助手,我可以使用如下......
<mytaghelper attr1="jim"></mytaghelper>
Run Code Online (Sandbox Code Playgroud)
我希望能够将其缩短为...
<mytaghelper attr1="jim">
Run Code Online (Sandbox Code Playgroud)
...或者至少...
<mytaghelper attr1="jim"/>
Run Code Online (Sandbox Code Playgroud)
但是,我无法让它发挥作用。这是 Process 方法的一些示例代码...
public override void Process(TagHelperContext context, TagHelperOutput output) {
output.TagName = "div";
output.PreContent.SetHtmlContent("<div>");
output.Content.SetHtmlContent("OUTPUT HTML GOES HERE");
output.PostContent.SetHtmlContent("</div>");
output.Attributes.Clear();
}
Run Code Online (Sandbox Code Playgroud)
我曾尝试向类上的属性添加TagStructure设置HtmlTargetElement...
[HtmlTargetElement("mytaghelper", TagStructure = TagStructure.WithoutEndTag)]
Run Code Online (Sandbox Code Playgroud)
……不过好像没什么区别。<mytaghelper attr1="jim"/>生成<div />并<mytaghelper attr1="jim"></mytaghelper>生成<div></mytaghelper>.
如果我设置TagStructure为NormalOrSelfClosing然后包含一个结束标记有效,但<mytaghelper attr1="jim"/>给出一个空的<div />
任何人都能够解释我需要做什么?
我正在玩MVC 6/ASP.Net vNext中创建自定义标记帮助器 - taghelper工作正常,但有没有办法指定有效的asp属性与标签一起使用,以便它们出现在intellisense中?例如,我希望在视图中添加与我的taghelper标准匹配的标记时,asp-ajax和asp-onsuccess会出现在intellisense列表中:
[TargetElement("form", Attributes = AjaxForm)]
public class UnobtrusiveFormTagHelper : TagHelper
{
private const string AjaxForm = "asp-ajax";
public override void Process(TagHelperContext context, TagHelperOutput output)
{
base.Process(context, output);
output.Attributes.Add("data-ajax", true);
output.Attributes.Add("data-onsuccess", context.AllAttributes["asp-onsuccess"]);
}
}
Run Code Online (Sandbox Code Playgroud)
用法:
<form asp-ajax="true" asp-onsuccess="dothis();">
Run Code Online (Sandbox Code Playgroud)
提前致谢
tag-helpers ×10
asp.net-core ×6
c# ×4
asp.net ×3
asp.net-mvc ×3
razor ×2
fallback ×1
javascript ×1
jquery ×1