Home > Software design >  Iframe in asp.net core
Iframe in asp.net core

Time:02-18

I have existing code in C# class to create iframe written old .net framework. I have to implement the same in asp.net core. *Existing C# code in .Net framework ( .aspx.cs)

{
   HtmlGenericControl framecntymap = new HtmlGenericControl("iFrame");
            framecntymap.Attributes.Add("width", "310px");
            framecntymap.Attributes.Add("height", "310px");
            framecntymap.Attributes.Add("frameborder", "0");
            framecntymap.Attributes.Add("src", "displayTest.html");
            PanelCountyMap.Controls.Add(framecntymap);


   
}

The below code is not working in asp.net core (*.chtml) class

   <iframe name="myIframe" id="myIframe" width="400px" height="400px" runat="server" src="~/GIS/displayTest.html"></iframe>

The src will be dynamic. I am going to update src in jquery.

How can implement similar functionality in asp.net core

Thanks

CodePudding user response:

In ASP.NET Core, you can use @Html.Raw(xx) to generate string/object to html format.

Here is a whole working demo:

public class HomeController : Controller
{
    public async Task<IActionResult> Index()
    {
        ViewData["Iframe"] = @"<iframe name='myIframe' id='myIframe' width='200' height='200' src='/Home/Privacy'></iframe>";
        return View();           
    }
    public async Task<IActionResult> Privacy()
    {
        return View();
    }
}

Index.cshtml:

@Html.Raw(ViewData["Iframe"])

Privacy.cshtml:

<h1>Privacy</h1>

Result:

enter image description here

  • Related