asp.net MVC3 根据页面内容自动生成元关键字和描述

发布于 2024-10-31 19:01:16 字数 1837 浏览 1 评论 0原文

我正在使用 razor 视图引擎开发 MVC3 Web 应用程序。我正在寻找一种在我的页面上自动生成元关键字和描述的解决方案。

我已经找到 stackoverflow 上的这个解决方案,这个想法看起来不错。但由于我计划在我的网站上发布博客等...我希望根据页面内容自动生成元关键字和描述。

到目前为止,我想到了以下几点。假设当前的 HTML 如下所示;

   <html>
    <head>
    </head>
        <body>
            <header></header>
            <div id="container">
                <div id="sideBar"></div>
                <div id="pageHeader"></div>
                <div id="content">
                    <!--This part contains the dynamic content-->
                </div>
                <div class="clear"></div>
            </div>
            <footer></footer>
        </body>
    </html>

我想要的情况如下所示;

   <html>
    <head>
        @Html.MetaKeywords()
        @Html.MetaDescription()
    </head>
        <body>
            <header></header>
            <div id="container">
                <div id="sideBar"></div>
                <div id="pageHeader"></div>
                <div id="content">
                    <!--This part contains the dynamic content-->
                </div>
                <div class="clear"></div>
            </div>
            <footer></footer>
        </body>
    </html>

说了这么多,我还有以下问题没有得到解答,希望有人能提供帮助。

  1. 上面描述的情况,使用 HTML 帮助程序。这是根据内容在每个页面上生成关键字和描述的良好逻辑方法吗?
  2. 如果是这样,有人有经验或者是一个很好的例子吗?
  3. 如果没有,还有其他好的选择吗?然后在每个控制器操作上使用一个属性?这仍然是一个选项,但它不会根据我的页面内容生成关键字和描述。

我希望有人能帮助我。谢谢!

I'm working on an MVC3 web application using the razor view engine. I'm looking for a solution to generate meta keywords and description automatically on my pages.

I've already found this solution here on stackoverflow, the idea is looking good. But since I'm planning to post blogs etc... on my website I want the meta keywords and description to be auto generated based on the content of the page.

Soo far I've got the following things in mind. Let's say that the current HTML looks like below;

   <html>
    <head>
    </head>
        <body>
            <header></header>
            <div id="container">
                <div id="sideBar"></div>
                <div id="pageHeader"></div>
                <div id="content">
                    <!--This part contains the dynamic content-->
                </div>
                <div class="clear"></div>
            </div>
            <footer></footer>
        </body>
    </html>

The desired situation for me would be something like below;

   <html>
    <head>
        @Html.MetaKeywords()
        @Html.MetaDescription()
    </head>
        <body>
            <header></header>
            <div id="container">
                <div id="sideBar"></div>
                <div id="pageHeader"></div>
                <div id="content">
                    <!--This part contains the dynamic content-->
                </div>
                <div class="clear"></div>
            </div>
            <footer></footer>
        </body>
    </html>

Said all this I've got the following questions left unanswered, hope someone can help.

  1. The situation described above, with the HTML-helpers. Is this an good and logic approach to generate keywords and description on every single page, based on the content?
  2. If this is, does anyone has experience and maybe an good example?
  3. If not, are there any other good options? Other then using an attribute on each controller action? This still is an option, but it doesn't generate keywords an description based on my page content.

I hope someone can help me out. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

晨光如昨 2024-11-07 19:01:16

如果我理解正确,您希望能够根据内容 div 内的文本自动生成元数据?

我会尝试将该文本/html/模型传递到 MetaKeywords 和 MetaDescription 函数中,并允许它们解析/分析/查找您需要放入元数据中的信息。

   <html>
    <head>
        @Html.MetaKeywords(Model.ContentText)
        @Html.MetaDescription(Model.ContentText)
    </head>
        <body>
            <header></header>
            <div id="container">
                <div id="sideBar"></div>
                <div id="pageHeader"></div>
                <div id="content">
                    <!--This part contains the dynamic content-->
                </div>
                <div class="clear"></div>
            </div>
            <footer></footer>
        </body>
    </html>

If I'm understanding correctly, you want to be able to auto generate the metadata based on the text inside the content div?

I would try passing that text / html / model into the MetaKeywords and MetaDescription functions, and allowing them to parse / analyze / look-up the information you need to put in the metadata.

   <html>
    <head>
        @Html.MetaKeywords(Model.ContentText)
        @Html.MetaDescription(Model.ContentText)
    </head>
        <body>
            <header></header>
            <div id="container">
                <div id="sideBar"></div>
                <div id="pageHeader"></div>
                <div id="content">
                    <!--This part contains the dynamic content-->
                </div>
                <div class="clear"></div>
            </div>
            <footer></footer>
        </body>
    </html>
追星践月 2024-11-07 19:01:16

您应该了解生成页面内容是操作处理的最后阶段,它直接执行到 HttpContextBase.Response 并且通常服务器可能永远不会缓冲整个内容,它可能会由于生成而发送到客户端。

您可以按照此处所述在全局注册您的过滤器 http://weblogs.asp.net/gunnarpeipman/archive/2010/08/15/asp-net-mvc-3-global-action-filters.aspx 所以你不需要需要为每个操作方法添加属性。

You should understand producing page content is the last stage of action processing, it performes directly to HttpContextBase.Response and generally server may never have whole content buffered, it may be sent to client due producing.

You may register your filter globally as described here http://weblogs.asp.net/gunnarpeipman/archive/2010/08/15/asp-net-mvc-3-global-action-filters.aspx so you don't need to put attribute on each action method.

绾颜 2024-11-07 19:01:16

在 _layout.cshtml 上,您可以在标题部分添加 @Rendersection("header", false) !暂时是假的。
稍后您必须将其更改为 true 或删除所需的属性。

在您现在可以使用的每个页面上,

@section header{
<meta name="Author" content="1SeoAdvies.nl" />;

Here you add every desired meta tag.

}

请注意 _layout 页面的标题部分中没有元标记。

On the _layout.cshtml you can add a @Rendersection("header", false) In the header section!! False for the time being.
Later you have to change this to true or remove the required attribute.

On each page you can use now

@section header{
<meta name="Author" content="1SeoAdvies.nl" />;

Here you add every desired meta tag.

}

beware you don't have meta tags in the header section on the _layout page.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文