2

I have implemented the project of migrating Asp.Net MVC into Asp.Net Core 3.1.

HomeController.cs

   static Regex regex = new Regex("(</?)script", RegexOptions.IgnoreCase);
   public static MvcHtmlString EmbeddedTemplate(EntityBase entityBase, MvcHtmlString template, string defaultString)
    {
        return MvcHtmlString.Create("<script type=\"template\" id=\"{0}\" data-toString=\"{2}\">{1}</script>"
                            .Formato(entityBase.Compose(EntityBaseKeys.Template),
                            regex.Replace(template.ToHtmlString(), m => m.Value + "X"),
                            defaultString));
    }

I have followed the link: .NET CORE MvcHtmlString(TextboxFor, LabelFor).ToString() Error. Here it is suggested to use IHtmlContent(Interface). But it does not have a Create() method.

And also what is the equivalent of MvcHtmlString? Kindly suggest on alternative solution on this.

Karthic G
  • 1,162
  • 1
  • 14
  • 31
  • Did you check this [question](https://stackoverflow.com/questions/29602202/equivalent-for-mvchtmlstring-in-asp-net-5)? Is says about `HtmlString` class, which exist in `Microsoft.AspNetCore.Html` namespace in .Net core – Pavel Anikhouski Nov 02 '20 at 14:45
  • Yes checked but it doesn't have create() method – Karthic G Nov 05 '20 at 14:03

1 Answers1

4

Finally I got it. Asp.Net Core replaced MvcHtmlString for a new HtmlString type.

static Regex regex = new Regex("(</?)script", RegexOptions.IgnoreCase);
public static HtmlString EmbeddedTemplate(EntityBase entityBase, MvcHtmlString template, string defaultString)
{
    return new HtmlString("<script type=\"template\" id=\"{0}\" data-toString=\"{2}\">{1}</script>"
                        .Formato(entityBase.Compose(EntityBaseKeys.Template),
                        regex.Replace(template.ToHtmlString(), m => m.Value + "X"),
                        defaultString));
}

Additionally, you need to declare the using directive

using Microsoft.AspNetCore.Html;
Karthic G
  • 1,162
  • 1
  • 14
  • 31