我想将简单/文字返回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内容而没有机器人内容。
如果所有内容都放入<div ui-view="main" class="shuffle-animation"></div>
答案 0 :(得分:1)
这与AngularJS没什么关系。
不要直接写入响应流,让ASP.NET MVC处理它:
return Content("whatever you want", "text/plain");
这样你就可以从方法中获取字符串。