gpt4 book ai didi

c# - Asp.Net MVC 中的项目之间共享声明性 html 帮助程序

转载 作者:行者123 更新时间:2023-11-30 12:47:55 27 4
gpt4 key购买 nike

是否可以将声明性 html 帮助程序放在不同于应用程序 Web 项目的程序集中?

目前,我知道我唯一可以放置声明性 html 帮助程序的地方是我的应用程序 Web 项目的“App_Code”文件夹中。

经过几个月的开发,我现在有很多这样的声明性 html 助手,我必须在我的所有项目中共享它的唯一一个解决方案是一个非常糟糕的做法 'Copy Paste Driven Development' .

还有其他方法吗?

附言

只是不要被什么是声明性 html 帮助程序的定义所混淆,这里是一个代码示例,与扩展 System.Web.Mvc.Html 类型的 c# 扩展相比有所不同:

@using Microsoft.Web.Mvc.Html

@helper YesNoRadioButton(System.Web.Mvc.HtmlHelper html, string name, bool value, bool disable = false)
{
@html.RadioButton(name, true, value, id: name.Replace(".", "_") + "_Yes", disabled: disable) @:Oui
@html.RadioButton(name, false, !value, id: name.Replace(".", "_") + "_No", disabled: disable) @:Non
}

@helper YesNoRadioButton(System.Web.Mvc.HtmlHelper html, string name, bool? value, bool disable = false)
{
@html.RadioButton(name, true, value.HasValue && value.Value, id: name.Replace(".", "_") + "_Yes", disabled: disable) @:Oui
@html.RadioButton(name, false, value.HasValue && !value.Value, id: name.Replace(".", "_") + "_No", disabled: disable) @:Non
}

enter image description here

最佳答案

为这些助手创建一个类库,然后在项目之间共享它。如果将助手放在自定义创建的文件夹 Helpers 中会更好。这就是我所做的。

可以引用这个答案:Where shall I put my utilities classes in a ASP.NET MVC3 application?

关于c# - Asp.Net MVC 中的项目之间共享声明性 html 帮助程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15346222/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com