我将创建一个 Controller ,该 Controller 响应通过 IHtmlHelper 接口(interface)创建的第三方 IHtmlContent 而不使用 View() 方法。实际上我正在使用一种解决方法:
我创建了 IHtmlHelper 的扩展函数,其行为类似于“主”,而其他静态函数的行为类似于“奴隶”。主函数根据它作为参数接收的数据结构调用从属函数。从站基于第三方库创建 IHtmlContent。当所有从机功能完成后, Controller 发回响应。
此行为在 Controller 调用的 cshtml View 中。
是否可以使用依赖注入(inject)将 IHtmlHelper 作为 Controller 构造函数参数?
就像是
public class MyTestController
{
private readonly IHtmlHelper _html;
public MyTestController(IHtmlHelper html) {
_html = html;
}
}
如果可能的话,我会简化许多操作,因为我正在使用这个主/从功能来复制一个类的行为
在这里,您可以找到我的代码现在如何工作以及我想如何更改它的纯示例
https://gist.github.com/Blackleones/eb0d02b9dd99164271af88e22143d72b#file-example-cs
我需要这个组件,因为第三方库是 IHtmlHelper 的扩展
请您参考如下方法:
我感谢 Itminus 回答我。
但是,在 Controller 构造函数上调用 IHtmlHelper 是不够的,您必须在使用 IHtmlHelper 之前对其进行上下文化。
我想分享我找到的另一个解决方案并解决我的问题,也许它对其他用户有用。
请看 https://gist.github.com/Blackleones/eb0d02b9dd99164271af88e22143d72b#file-example-cs记住我的目标是什么。
public class MyHtmlHelperAdapter
{
public IHtmlHelper _html;
public NubessHtmlHelperAdapter(IHtmlHelper html)
{
_html = html;
}
public IHtmlHelper Html => _html;
public ViewContext ViewContext => _html.ViewContext;
public HttpContext HttpContext => _html.ViewContext.HttpContext;
public ViewDataDictionary ViewData => _html.ViewData;
public IServiceProvider provider => _html.ViewContext.HttpContext.RequestServices;
}
public static class MyBuilderExtension
{
public static MyBuilder Nubess(this IHtmlHelper html)
{
return new MyBuilder(new MyHtmlHelperAdapter(html));
}
}
MyBuilder 在哪里
public class MyBuilder
{
private readonly MyHtmlHelperAdapter _htmlHelper;
public MyBuilder(MyHtmlHelperAdapter htmlHelper)
{
_htmlHelper = htmlHelper;
}
public FormBuilder<object> Form<T>(IDictionary<string, string> customizeArgs = null, FormBuilder<object> externalForm = null)
{
return new FormBuilder(_htmlHelper, typeof(T), customizeArgs, externalForm).Build();
}
}
public class FormBuilder
{
private MyHtmlHelperAdapter _htmlHelper;
private readonly IStringLocalizer<SharedResources> _localizer;
private readonly LinkGenerator _linkGenerator;
private readonly IEngine _engine;
private readonly IEngineElement _element;
private readonly Type _typeDescriptor;
private readonly IDictionary<string, string> _descriptorArgs;
/* variabili per semplificare la gestione del builder */
private readonly FormBuilder<object> Form;
private readonly FormConfig Config;
private readonly IList<FormGroupConfig> Groups;
private readonly IList<FormItemConfig> Items;
private readonly IDictionary<string, string> FormOptions;
private readonly string _clientPrefix = "smtForm_{0}_";
public FormBuilder(MyHtmlHelperAdapter htmlHelper, Type typeDescriptor, IDictionary<string, string> ModelCustomizeArgs = null, FormBuilder<object> externalForm = null)
{
_htmlHelper = htmlHelper;
_localizer = _htmlHelper.provider.GetRequiredService<IStringLocalizer<SharedResources>>() ?? throw new ArgumentNullException();
_linkGenerator = _htmlHelper.provider.GetRequiredService<LinkGenerator>() ?? throw new ArgumentNullException();
_engine = _htmlHelper.provider.GetRequiredService<IEngine>() ?? throw new ArgumentNullException();
//code..
}
public FormBuilder<object> Build()
{
//code..
return Form;
}
//methods..
}




