情节:从robot.txt验证内容
在我的多站点应用程序中,我需要为每个站点提供一个robot.txt文件。此实现如下:
1-在开始页面中包含类型TextArea的RobotsContent属性。
2-添加了下面给出的操作器,并使用处理程序的Web配置条目。
public void ProcessRequest(HttpContext context)
{
var uri = context.Request.Url;
var currentSite = _siteDefinitionRepository.List().FirstOrDefault(siteDefinition => siteDefinition.Hosts.Any(hostDefinition => hostDefinition.Authority.Hostname.Equals(uri.Host)));
if (currentSite != null)
{
var startPage = _contentLoader.Get<StartPage>(currentSite.StartPage);
var robotsContentProperty = startPage.RobotsContent;
// Generate robots.txt file
// Set the response code, content type and appropriate robots file here
if (!string.IsNullOrEmpty(robotsContentProperty))
{
context.Response.ContentType = "text/plain";
context.Response.Write(robotsContentProperty);
context.Response.StatusCode = 200;
context.Response.End();
}
}
}
我知道有一些Nuget软件包可用于处理robot.txt,但出于某些原因&amp;需要对此进行更多控制,我创建了一个自定义。上述工作原状。
refeReing https://developers.google.com/search/search/docs /高级/机器人/create-robots-txt
它提到规则案例敏感,有组织(用户代理,允许,禁止,禁止),指令(用户代理,允许,禁止,禁止)需要。所有这些规则都适当&amp;这是一个免费的文本方面,我可以在此内添加任何随机的东西。因此,我可以对此进行任何验证吗?在线验证可以避免,但是在发布文本时,有什么方法可以验证文本。
In my multisite application, I need to include a robot.txt file for each of the site. The implementation for this goes as follows:
1- Included a RobotsContent property of type textarea within the Start page.
2- Added a hander as given below with a web config entry for the handler.
public void ProcessRequest(HttpContext context)
{
var uri = context.Request.Url;
var currentSite = _siteDefinitionRepository.List().FirstOrDefault(siteDefinition => siteDefinition.Hosts.Any(hostDefinition => hostDefinition.Authority.Hostname.Equals(uri.Host)));
if (currentSite != null)
{
var startPage = _contentLoader.Get<StartPage>(currentSite.StartPage);
var robotsContentProperty = startPage.RobotsContent;
// Generate robots.txt file
// Set the response code, content type and appropriate robots file here
if (!string.IsNullOrEmpty(robotsContentProperty))
{
context.Response.ContentType = "text/plain";
context.Response.Write(robotsContentProperty);
context.Response.StatusCode = 200;
context.Response.End();
}
}
}
I am aware there are a few nuget packages available for handling robot.txt but for some reasons & the need to have more control on this one ,I created a custom one. The above works as expected.
Referreing https://developers.google.com/search/docs/advanced/robots/create-robots-txt
It mentions that the rules are case sensitive ,comes in a group(user-agent, allow, disallow),directives(user-agent, allow, disallow )are required. With all these rules in place & this being a free textarea,I can add any random stuff within this.So is there any validations that I can apply to this?There are online validations avaliable for this but is there any way I can validate the text when it is being published.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在robotscontent 属性上实现Episerver验证属性。
如果使用在线验证器不是一个选项,则可以通过
validate
属性实现方法中的正则表达式来处理。You can implement an EPiServer validation attribute and use that on your
RobotsContent
property.If using an online validator is not an option this could be handled by a i.e. a regular expression inside the
Validate
method of the attribute implementation.