本文介绍了如何在AngularJS中返回纯文本/文本robots.txt的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to return plain/text to 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视图

Angularjs View

          <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:

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内容.

When I hit http://domain/robots.txt, it does hit common controller, but it just return all full html content without robots content.

当一切似乎都放入< div ui-view ="main" class ="shuffle-animation"<//div>

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

推荐答案

与AngularJS无关.

This doesn't have much to do with AngularJS.

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

Do not write directly to the response stream, let ASP.NET MVC handle it:

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

那样,您只需从方法中获取字符串即可.

That way you will just get the string out from the method.

这篇关于如何在AngularJS中返回纯文本/文本robots.txt的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-15 08:22