【问题标题】:How to return plain/text robots.txt in AngularJS如何在 AngularJS 中返回纯文本 robots.txt
【发布时间】:2015-05-13 06:45:28
【问题描述】:

我想将纯文本/文本返回到http://domain.com/robots.txt

asp.net mvc Common controller         

      public ActionResult RobotsTextFile()
        {
            var disallowPaths = new List<string>()
                                    {
                                        "/bin/",
                                        "/content/files/",
                                        "/content/files/exportimport/",
                                        "/country/getstatesbycountryid",
                                        "/install",
                                        "/setproductreviewhelpfulness",
                                    };
            var localizableDisallowPaths = new List<string>()
                                               {
                                                   "/addproducttocart/catalog/",
                                                   "/addproducttocart/details/",
                                                   "/boards/forumwatch",                                                  
                                                   "/deletepm",
                                                   "/emailwishlist",
                                                   "/inboxupdate",
                                                   ...
                                               };


            const string newLine = "\r\n"; //Environment.NewLine
            var sb = new StringBuilder();
            sb.Append("User-agent: *");
            sb.Append(newLine);
            //sitemaps
            if (_localizationSettings.SeoFriendlyUrlsForLanguagesEnabled)
            {
                //URLs are localizable. Append SEO code
                foreach (var language in _languageService.GetAllLanguages(storeId: _storeContext.CurrentStore.Id))
                {
                    sb.AppendFormat("Sitemap: {0}{1}/sitemap.xml", _storeContext.CurrentStore.Url, language.UniqueSeoCode);
                    sb.Append(newLine);
                }
            }
            else
            {
                //localizable paths (without SEO code)
                sb.AppendFormat("Sitemap: {0}sitemap.xml", _storeContext.CurrentStore.Url);
                sb.Append(newLine);
            }

            //usual paths
            foreach (var path in disallowPaths)
            {
                sb.AppendFormat("Disallow: {0}", path);
                sb.Append(newLine);
            }
            //localizable paths (without SEO code)
            foreach (var path in localizableDisallowPaths)
            {
                sb.AppendFormat("Disallow: {0}", path);
                sb.Append(newLine);
            }
            if (_localizationSettings.SeoFriendlyUrlsForLanguagesEnabled)
            {
                //URLs are localizable. Append SEO code
                foreach (var language in _languageService.GetAllLanguages(storeId: _storeContext.CurrentStore.Id))
                {
                    foreach (var path in localizableDisallowPaths)
                    {
                        sb.AppendFormat("Disallow: {0}{1}", language.UniqueSeoCode, path);
                        sb.Append(newLine);
                    }
                }
            }

            Response.ContentType = "text/plain";
            Response.Write(sb.ToString());
            return null;
        }

Angularjs 视图

          <html> <body>... script code here.. 
        <div class="content col-lg-9">
                    <div ui-view="main" class="shuffle-animation"></div>
                </div>
       <footer>...</footer>
    </body></html

ui.router:

$stateProvider           
                .state('robots', {
                    url: '/robots.txt',
                    views: {
                        '@': {
                            templateUrl: '/App/Main/layouts/_ColumnsTwo.html'
                        },
                        'main@root': {
                            templateUrl:  '/Common/RobotsTextFile' 
                   }
                    }
                })

当我点击http://domain/robots.txt 时,它确实点击了通用控制器,但它只返回了所有完整的 html 内容,没有机器人内容。

当一切似乎都放入 &lt;div ui-view="main" class="shuffle-animation"&gt;&lt;/div&gt; 时,我该怎么做才能在 Angularjs 中只推出纯文本/文本

结果应该类似于http://demo.nopcommerce.com/robots.txt

【问题讨论】:

    标签: c# asp.net-mvc angularjs nopcommerce


    【解决方案1】:

    这与 AngularJS 没有太大关系。

    不要直接写入响应流,让 ASP.NET MVC 处理:

    return Content("whatever you want", "text/plain");
    

    这样你就可以从方法中取出字符串了。

    【讨论】:

    • 返回内容不会只给我纯文本。它仍然从母版页携带 html 正文。在 MVC 中,Response.ContentType = "text/plain"; Response.Write(sb.ToString());将忽略网站页面结构。但现在它在 Angular 中不起作用
    • ContentResult 不会导致布局被调用,因为没有 View。它只返回你想要的内容。而 AngularJS 只是在浏览器中运行,所以既然没有 View,就不用担心 AngularJS。如果您从 ContentResult 获取视图,则您的设置有很大问题。
    • 愚蠢的我,我通过将路由放入 ui-route 进行了编译,只需使用 mvc 就足够了。感谢您的帮助。
    猜你喜欢
    • 2016-01-29
    • 1970-01-01
    • 2015-06-05
    • 2022-01-03
    • 2016-06-15
    • 1970-01-01
    • 2015-08-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多