Search code examples
htmlasp.netviewport

how to using html tags on asp.net projects


I am trying to use viewport on my asp.net project using this tag

<asp:HtmlMeta runat="server" ID="viewPort" name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0" />

I got this error

Parser Error

Description: An error occurred during the parsing of a resource required to service this request.

Please review the following specific parse error details and modify your source file appropriately.

Parser Error Message:

Unknown server tag 'asp:HtmlMeta'.

Is there any solution?


Solution

  • It looks like that's not how the HtmlMeta class is intended to be used.

    For starters, if you don't need programmatic access and just need a <meta> element, then just use one:

    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
    

    That is, unless you need to interact with this from your server-side code, there's no reason to overcomplicate it. Just use the HTML normally.

    On the other hand, if you do need programmatic access (such as to dynamically set the content based on some condition), you'd use the HtmlMeta class in your server-side code. For example:

    protected void Page_Load(object sender, EventArgs e)
    {
        HtmlMeta viewport = new HtmlMeta();
        viewport.Name = "viewport";
        viewport.Content = "width=device-width, initial-scale=1.0, maximum-scale=1.0";
    
        HtmlHead header = (HtmlHead)Page.Header;
        header.Controls.Add(viewport);
    }