在ASP.NET中插入HTML标题和META描述服务器端



我正在使用iis 7和asp.net。

我想做的是能够服务器端更改 title 和我的index.html文件的 meta description ,然后在webbrowser中显示。

<title>Replace with text here on server</title>
<meta name="description" content="Replace with text here on server">

我知道我可以在ASP.NET中创建HTTP处理程序,HTTP模块或HTTP过滤器,以根据请求进行此操作。我找到了这种方式附加html的示例,但是没有一个显示如何替换/插入现有html的文本。

我想伪代码看起来像这样:

Process_Request(file, content)
{
   if (file == index.html)
   {
      content.replace("<title>xxx</title>", "<title>This is what I want title to be</title>"));
      ...same replace for description
   }
   return content to client;
}

我该怎么做?

解决方案

我根据本文中的代码和示例创建了一个内容解析器模块。不幸的是,作者忽略了将模块添加到web.config的部分,因此您应该记住自己实现该部分。

我所做的是将标题和描述更改为基于URL段和数据库查找的写入方法。

服务器端,您可以在主页的page_load事件中设置page.title以更改标题。

另外,服务器端,使用htmlmeta类,如下所述更改元属性:https://msdn.microsoft.com/en-us/library/system.web.ui.htmlcontrols.htmlmeta).aspx

在Angular中,您可以在浏览器平台中使用标题服务的设置方法:https://angular.io/guide/set-document-title

另外,Angular浏览器平台,使用Meta Service AddTag方法来操纵Angular:https://angular.io/api/platform-browser/meta

最新更新