如何使用代码设置html head section的标签 [英] how to set tags of html head section using code

查看:120
本文介绍了如何使用代码设置html head section的标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目是一个asp.net网页,其网页布局包括页面部分。我这样做:

My project is an asp.net webpages website which has a layout including head section of pages. I wana do this:

<head>
   <link rel="canonical" href="@App.PathHome" />
</head>



但不在布局中。我通过主页中的asp.net代码动态添加它。语法如何?

现在我正在尝试这个:


but not in layout. I wana add it dynamically via asp.net code in the home page. How is the syntax?
Now I am trying this:

//Define an HtmlLink control.
HtmlLink myHtmlLink = new HtmlLink();
myHtmlLink.Href = "/Default";
myHtmlLink.Attributes.Add("rel", "canonical");
// Add the HtmlLink to the Head section of the page.
Page.Header.Controls.Add(myHtmlLink);



但是为最后一个代码行收到错误无法对空引用执行运行时绑定。


but getting the error "Cannot perform runtime binding on a null reference" for the last code line.

推荐答案

我不确定你可以在WebMatrix中使用控件。

无论如何,也许你可以通过使用部分完成你的任务。

如果添加行

I''m not sure you can use controls in WebMatrix.
Anyway, maybe you can accomplish your task with the use of sections.
If you add the row
@RenderSection("otherlinks", required: false)



在您网站的头部布局页面和


in the head of your site layout page and

@section otherlinks {
    <link rel="canonical" href="@App.PathHome" />
}



,生成的页面应该有效。


in your child page, the resulting page should work.


这篇关于如何使用代码设置html head section的标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆