ASP.NET 5中System.Web.Mvc.Html.InputExtensions的等价物是什么?

ASP.NET 4中使用的ASP.NET 5等效的System.Web.Mvc.Html.InputExtensions是什么?

见下面的例子:

 public static class CustomHelpers { // Submit Button Helper public static MvcHtmlString SubmitButton(this HtmlHelper helper, string buttonText) { string str = ""; return new MvcHtmlString(str); } // Readonly Strongly-Typed TextBox Helper public static MvcHtmlString TextBoxFor(this HtmlHelper htmlHelper, Expression<Func> expression, bool isReadonly) { MvcHtmlString html = default(MvcHtmlString); if (isReadonly) { html = System.Web.Mvc.Html.InputExtensions.TextBoxFor(htmlHelper, expression, new { @class = "readOnly", @readonly = "read-only" }); } else { html = System.Web.Mvc.Html.InputExtensions.TextBoxFor(htmlHelper, expression); } return html; } } 

对于ASP.NET 4代码:

  MvcHtmlString html = System.Web.Mvc.Html.InputExtensions.TextBoxFor( htmlHelper, expression); 

ASP.NET 5的等价物是:

 Microsoft.AspNet.Mvc.Rendering.HtmlString html = (Microsoft.AspNet.Mvc.Rendering.HtmlString) Microsoft.AspNet.Mvc.Rendering.HtmlHelperInputExtensions.TextBoxFor( htmlHelper, expression); 

或者包含在页面中的命名空间

 @Microsoft.AspNet.Mvc.Rendering; 

它写道:

 HtmlString html = (HtmlString)HtmlHelperInputExtensions.TextBoxFor(htmlHelper,expression); 

请注意,它的返回类型是一个接口IHtmlContent而不是ASP.NET 4中的MvcHtmlString

MvcHtmlString已被ASP.NET 5中的HtmlString取代。

由于返回了HtmlString的接口IHtmlContent而不是HtmlString本身,因此必须将返回值转换为HtmlString

但是,您希望在ASP.NET 5中将其用作扩展方法,因此您应该将方法返回类型更改为IHtmlContent并将代码更改为:

  IHtmlContent html = HtmlHelperInputExtensions.TextBoxFor(htmlHelper, expression); return html; 

源代码可以在这里找到。