情节:从robot.txt验证内容

发布于 2025-01-20 17:12:05 字数 1665 浏览 5 评论 0原文

在我的多站点应用程序中,我需要为每个站点提供一个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半枫 2025-01-27 17:12:05

您可以在robotscontent 属性上实现Episerver验证属性。

using EpiServer.Validation
public class RobotTxtValidatorAttribute : IValidate<StartPage>
{
    public IEnumerable<ValidationError> Validate(StartPage startPage)
    {
        // Validate the property value here, i.e. by using an HttpClient to use the online validation that you mentioned.
    }
}
public class StartPage
{
    [RobotTxtValidator]
    public string RobotsContent { get; set; }
}

如果使用在线验证器不是一个选项,则可以通过validate属性实现方法中的正则表达式来处理。

You can implement an EPiServer validation attribute and use that on your RobotsContent property.

using EpiServer.Validation
public class RobotTxtValidatorAttribute : IValidate<StartPage>
{
    public IEnumerable<ValidationError> Validate(StartPage startPage)
    {
        // Validate the property value here, i.e. by using an HttpClient to use the online validation that you mentioned.
    }
}
public class StartPage
{
    [RobotTxtValidator]
    public string RobotsContent { get; set; }
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文