asp.net操作Meta tages/Style/title/Header

动态添加网页元素
本文介绍如何使用C#动态地为网页添加元数据标签、样式表、内联样式及标题等元素,通过具体代码示例展示了如何操作ASP.NET页面的Header部分。

动态添加Meta tages:

  1. //Render:<metaname="keywords"content="Somewordslistedhere"/>
  2. HtmlMetameta=newHtmlMeta();
  3. meta.Name="keywords";
  4. meta.Content="Somewordslistedhere";
  5. this.Header.Controls.Add(meta);

  6. //Render:<metaname="robots"content="noindex"/>
  7. meta=newHtmlMeta();
  8. meta.Name="robots";
  9. meta.Content="noindex";
  10. this.Header.Controls.Add(meta);

  11. //Render:<metaname="date"content="2006-03-25"scheme="YYYY-MM-DD"/>
  12. meta=newHtmlMeta();
  13. meta.Name="date";
  14. meta.Content=DateTime.Now.ToString("yyyy-MM-dd");
  15. meta.Scheme="YYYY-MM-DD";
  16. this.Header.Controls.Add(meta);

动态添加样式表:

  1. HtmlLinklink=newHtmlLink();
  2. link.Attributes.Add("type","text/css");
  3. link.Attributes.Add("rel","stylesheet");
  4. link.Attributes.Add("href","~/style.css");
  5. this.Header.Controls.Add(link);

动态设置Style:

  1. Stylestyle=newStyle();
  2. style.ForeColor=System.Drawing.Color.Navy;
  3. style.BackColor=System.Drawing.Color.LightGray;

  4. //Addthestyletotheheaderforthebodyofthepage
  5. this.Header.StyleSheet.CreateStyleRule(style,null,"body");

设置Title:

  1. this.Header.Title="这是个Title测试";
  2. //MasterPage中用下面的代码
  3. //this.Page.Title=SiteMap.CurrentNode.Title;

MainLayout.vue <template> <el-container class="layout-container"> <el-aside width="200px"> <Sidebar /> </el-aside> <el-main> <TagsView /> <router-view /> </el-main> </el-container> </template> <script lang = "ts" setup name = "layout"> import Sidebar from '@/components/Sidebar.vue'; import TagsView from '@/components/Tags.vue'; </script> <style scoped> .header-content { display: flex; justify-content: space-between; align-items: center; height: 100%; } .right-section { display: flex; align-items: center; } .layout-container { display: flex; height: 100vh; } .el-aside { background: #04152e; height: 100vh; position: sticky; top: 0; } </style> Sidebar.vue <template> <div class="sidebar"> <div class="sidebar-logo flex-center"> <img src="@/assets/cat-home.jpeg" alt="Logo" class="logo-img" /> <span class="logo-text">Digital Twin</span> </div> <el-menu active-text-color="#fff" background-color="#001529" :default-active="$route.path" text-color="#999" :unique-opened="true" router> <SideBarItem :routerList="routerList" /> </el-menu> </div> </template> <script lang="ts" setup> import SideBarItem from './SidebarItem.vue'; import { useRouter } from 'vue-router'; const router = useRouter(); const routerList = router.getRoutes().filter((v) => v.meta && v.meta.isShow); </script> <style lang="scss" scoped> .sidebar { height: 100%; .sidebar-logo { height: 48px; background-color: #002140; color: #fff; font-weight: 700; line-height: 48px; text-align: center; font-size: 20px; display: flex; align-items: center; padding: 0 16px; box-sizing: border-box; .logo-img { height: 15px; width: 30px; margin-right: 10px; } .logo-text { font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; font-size: 18px; font-weight: bold; color: #ffffff; } } .el-menu { width: 100%; height: calc(100% - 48px); border-right: 0; overflow: auto; } } </style> SidebarItem.vue <template> <template v-for="item in routerList" :key="item.path"> <!-- 有子菜单:el-sub-menu --> <el-sub-menu :index="item.path" v-if="item.children && item.children.length > 0"> <template #title> <!-- 判断是否为 Iconify 格式的图标(包含冒号) --> <Icon v-if="item.meta.icon && typeof item.meta.icon === 'string' && item.meta.icon.includes(':')" :icon="item.meta.icon" width="18" height="18" style="margin-right: 6px; vertical-align: middle;" /> <!-- 否则当作 Vue 组件处理(如 Document, Setting 等) --> <el-icon v-else-if="item.meta.icon" :size="16" color="#409eff" style="margin-right: 6px; vertical-align: middle;" > <component :is="getIconComponent(item.meta.icon as string)" /> </el-icon> <span class="menu-title">{{ item.meta.title }}</span> </template> <!-- 递归子菜单 --> <sidebar-item :routerList="item.children as CustomRouteRecordRaw[]" /> </el-sub-menu> <!-- 叶子节点:el-menu-item --> <el-menu-item :index="item.path" v-else> <!-- 同样逻辑判断图标类型 --> <Icon v-if="item.meta.icon && typeof item.meta.icon === 'string' && item.meta.icon.includes(':')" :icon="item.meta.icon" width="18" height="18" style="margin-right: 6px; vertical-align: middle;" /> <el-icon v-else-if="item.meta.icon" :size="16" color="#409eff" style="margin-right: 6px; vertical-align: middle;" > <component :is="getIconComponent(item.meta.icon as string)" /> </el-icon> <span>{{ item.meta.title }}</span> </el-menu-item> </template> </template> <script setup lang="ts"> import * as ElementPlusIcons from '@element-plus/icons-vue' import type { RouteRecordRaw } from 'vue-router' type CustomRouteRecordRaw = RouteRecordRaw & { meta: { isShow?: boolean; }; }; const props = defineProps({ routerList: { type: Array as () => CustomRouteRecordRaw[], required: true } }); const getIconComponent = (iconName: string) => { return (ElementPlusIcons as Record<string, any>)[iconName] || null } </script> <style scoped lang="scss"> .is-active { background: #1f4a5e; font-weight: 700; } .menu-title { font-size: 16px; // 调整字体大小 font-weight: 700; // 可选:调整字体粗细 } .el-menu-item { font-size: 16px; &:hover { color: #fff; font-weight: 700; } } .el-menu--collapse { .el-menu-item { justify-content: center; } } </style> tages.vue <template> <div class="layout-header"> <div class="nav-collapse"> <el-breadcrumb separator="/"> <el-breadcrumb-item :to="{ path: '/' }">Home</el-breadcrumb-item> <el-breadcrumb-item v-for="(item, index) in matchedRoutes" :key="index"> {{ item.meta?.title }} </el-breadcrumb-item> </el-breadcrumb> <div class="user-dropdown-wrapper"> <UserDropdown /> </div> </div> <div class="tags-view-container"> <div class="tags-view-wrapper"> <router-link v-for="tag in visitedViews" :key="tag.path" :to="{ path: tag.path }" class="tags-view-item" :class="isActive(tag) ? 'active' : ''" > {{ tag.meta?.title }} <el-icon @click.prevent.stop="closeSelectedTag(tag)"> <Close /> </el-icon> </router-link> </div> </div> </div> </template> <script lang="ts" setup> import { computed } from 'vue' import { useRoute } from 'vue-router' import { useTagsViewStore } from '@/stores/tagsView' import type { RouteLocationNormalized } from 'vue-router' import UserDropdown from '@/components/UserDropdown.vue' const route = useRoute() const tagsViewStore = useTagsViewStore() const visitedViews = computed(() => tagsViewStore.visitedViews) const matchedRoutes = computed(() => route.matched.filter(item => item.meta?.title)) const isActive = (tag: RouteLocationNormalized) => tag.path === route.path const closeSelectedTag = (tag: RouteLocationNormalized) => { tagsViewStore.delView(tag) } </script> <style scoped> .layout-header { background: #fff; box-shadow: 0 1px 4px rgba(0,0,0,0.1); margin-top: -20px; margin-left: 0px; position: relative; } .nav-collapse { display: flex; align-items: center; padding: 0 15px; height: 50px; margin-left: 5px; width: 100%; } .user-dropdown-wrapper { margin-left: auto; } .nav-collapse.el-icon { margin-right: 15px; font-size: 20px; cursor: pointer; } .tags-view-container { height: 34px; background: #ffffff; padding-left: 15px; margin-top: 0px; border-bottom: 1px solid #e6e6e6; } .tags-view-wrapper { display: flex; justify-content: flex-start; padding-left: 5px; gap: 1px; } .tags-view-item { display: inline-flex; align-items: center; padding: 0 12px; border: 1px solid #dcdfe6; border-radius: 4px; font-size: 12px; line-height: 28px; background: #fff; box-shadow: 0 1px 2px rgba(0,0,0,0.1); transition: all 0.3s; } .tags-view-item:hover { border-color: #ffffff; } .tags-view-item.active { background-color: #5a9481; color: #fff; border-color: #f3f5f4; } .tags-view-item .el-icon { margin-left: 5px; cursor: pointer; } </style> 现在需要左侧导航栏点击后能向左侧折叠,并鼠标放在上面有提示
12-05
MainLayout.vue <el-main> <TagsView /> <router-view /> </el-main> </el-container> </template> <script lang = "ts" setup name = "layout"> import Sidebar from '@/components/Sidebar.vue'; import TagsView from '@/components/Tags.vue'; </script> <style scoped> .header-content { display: flex; justify-content: space-between; align-items: center; height: 100%; } .right-section { display: flex; align-items: center; } .layout-container { display: flex; height: 100vh; } .el-aside { background: #04152e; height: 100vh; position: sticky; top: 0; } </style> Sidebar.vue Logo Digital Twin <SideBarItem :routerList="routerList" /> </el-menu> </div> </template> <script lang="ts" setup> import SideBarItem from './SidebarItem.vue'; import { useRouter } from 'vue-router'; const router = useRouter(); const routerList = router.getRoutes().filter((v) => v.meta && v.meta.isShow); </script> <style lang="scss" scoped> .sidebar { height: 100%; .sidebar-logo { height: 48px; background-color: #002140; color: #fff; font-weight: 700; line-height: 48px; text-align: center; font-size: 20px; display: flex; align-items: center; padding: 0 16px; box-sizing: border-box; .logo-img { height: 15px; width: 30px; margin-right: 10px; } .logo-text { font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; font-size: 18px; font-weight: bold; color: #ffffff; } } .el-menu { width: 100%; height: calc(100% - 48px); border-right: 0; overflow: auto; } } </style> SidebarItem.vue <template #title> {{ item.meta.title }} <!-- 递归子菜单 --> <sidebar-item :routerList="item.children as CustomRouteRecordRaw[]" /> </el-sub-menu> <!-- 叶子节点:el-menu-item --> <el-menu-item :index="item.path" v-else> <!-- 同样逻辑判断图标类型 --> <Icon v-if="item.meta.icon && typeof item.meta.icon === 'string' && item.meta.icon.includes(':')" :icon="item.meta.icon" width="18" height="18" style="margin-right: 6px; vertical-align: middle;" /> <el-icon v-else-if="item.meta.icon" :size="16" color="#409eff" style="margin-right: 6px; vertical-align: middle;" > <component :is="getIconComponent(item.meta.icon as string)" /> </el-icon> <span>{{ item.meta.title }}</span> </el-menu-item> </template> </template> <script setup lang="ts"> import * as ElementPlusIcons from '@element-plus/icons-vue' import type { RouteRecordRaw } from 'vue-router' type CustomRouteRecordRaw = RouteRecordRaw & { meta: { isShow?: boolean; }; }; const props = defineProps({ routerList: { type: Array as () => CustomRouteRecordRaw[], required: true } }); const getIconComponent = (iconName: string) => { return (ElementPlusIcons as Record<string, any>)[iconName] || null } </script> <style scoped lang="scss"> .is-active { background: #1f4a5e; font-weight: 700; } .menu-title { font-size: 16px; // 调整字体大小 font-weight: 700; // 可选:调整字体粗细 } .el-menu-item { font-size: 16px; &:hover { color: #fff; font-weight: 700; } } .el-menu--collapse { .el-menu-item { justify-content: center; } } </style> tages.vue <template> <div class="layout-header"> <div class="nav-collapse"> <el-breadcrumb separator="/"> <el-breadcrumb-item :to="{ path: '/' }">Home</el-breadcrumb-item> <el-breadcrumb-item v-for="(item, index) in matchedRoutes" :key="index"> {{ item.meta?.title }} </el-breadcrumb-item> </el-breadcrumb> <div class="user-dropdown-wrapper"> <UserDropdown /> </div> </div> <div class="tags-view-container"> <div class="tags-view-wrapper"> <router-link v-for="tag in visitedViews" :key="tag.path" :to="{ path: tag.path }" class="tags-view-item" :class="isActive(tag) ? 'active' : ''" > {{ tag.meta?.title }} <el-icon @click.prevent.stop="closeSelectedTag(tag)"> <Close /> </el-icon> </router-link> </div> </div> </div> </template> <script lang="ts" setup> import { computed } from 'vue' import { useRoute } from 'vue-router' import { useTagsViewStore } from '@/stores/tagsView' import type { RouteLocationNormalized } from 'vue-router' import UserDropdown from '@/components/UserDropdown.vue' const route = useRoute() const tagsViewStore = useTagsViewStore() const visitedViews = computed(() => tagsViewStore.visitedViews) const matchedRoutes = computed(() => route.matched.filter(item => item.meta?.title)) const isActive = (tag: RouteLocationNormalized) => tag.path === route.path const closeSelectedTag = (tag: RouteLocationNormalized) => { tagsViewStore.delView(tag) } </script> <style scoped> .layout-header { background: #fff; box-shadow: 0 1px 4px rgba(0,0,0,0.1); margin-top: -20px; margin-left: 0px; position: relative; } .nav-collapse { display: flex; align-items: center; padding: 0 15px; height: 50px; margin-left: 5px; width: 100%; } .user-dropdown-wrapper { margin-left: auto; } .nav-collapse.el-icon { margin-right: 15px; font-size: 20px; cursor: pointer; } .tags-view-container { height: 34px; background: #ffffff; padding-left: 15px; margin-top: 0px; border-bottom: 1px solid #e6e6e6; } .tags-view-wrapper { display: flex; justify-content: flex-start; padding-left: 5px; gap: 1px; } .tags-view-item { display: inline-flex; align-items: center; padding: 0 12px; border: 1px solid #dcdfe6; border-radius: 4px; font-size: 12px; line-height: 28px; background: #fff; box-shadow: 0 1px 2px rgba(0,0,0,0.1); transition: all 0.3s; } .tags-view-item:hover { border-color: #ffffff; } .tags-view-item.active { background-color: #5a9481; color: #fff; border-color: #f3f5f4; } .tags-view-item .el-icon { margin-left: 5px; cursor: pointer; } </style> 现在需要左侧导航栏点击后能向左侧折叠,折叠按钮在tagsvue 的home左边,并鼠标放在上面有提示,右侧还要继续保持每打开一个网页有标签
最新发布
12-05
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值