How to return plain/text robots.txt in AngularJS

こ雲淡風輕ζ 提交于 2019-12-11 17:13:42

问题


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

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

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

What can I do to push out only plain/text in Angularjs when everything seems to put into <div ui-view="main" class="shuffle-animation"></div>

The result should be like http://demo.nopcommerce.com/robots.txt


回答1:


This doesn't have much to do with AngularJS.

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.



来源:https://stackoverflow.com/questions/30207503/how-to-return-plain-text-robots-txt-in-angularjs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!