如何覆盖nopcommerce网站的robots.txt?

时间:2018-08-20 05:35:18

标签: robots.txt nopcommerce

robots.txt NopCommerce 控制器生成。

我需要对其进行编辑或自定义。

我该怎么做?

尝试仅将我的普通robots.txt放置到网站的根目录-无法正常工作。

2 个答案:

答案 0 :(得分:1)

您可以从插件中覆盖 PrepareRobotsTextFile 工厂并在其中编写代码。

对于重写CommonModelFactory, 首先创建用于覆盖的类-RobotFactory.cs

 public class RobotFactory : CommonModelFactory
{
    #region Fields

    private readonly ICategoryService _categoryService;
    private readonly IProductService _productService;
    private readonly IManufacturerService _manufacturerService;
    private readonly ITopicService _topicService;
    private readonly ILanguageService _languageService;
    private readonly ICurrencyService _currencyService;
    private readonly ILocalizationService _localizationService;
    private readonly IWorkContext _workContext;
    private readonly IStoreContext _storeContext;
    private readonly ISitemapGenerator _sitemapGenerator;
    private readonly IThemeContext _themeContext;
    private readonly IThemeProvider _themeProvider;
    private readonly IForumService _forumservice;
    private readonly IGenericAttributeService _genericAttributeService;
    private readonly IWebHelper _webHelper;
    private readonly IPermissionService _permissionService;
    private readonly IStaticCacheManager _cacheManager;
    private readonly IPageHeadBuilder _pageHeadBuilder;
    private readonly IPictureService _pictureService;
    private readonly IHostingEnvironment _hostingEnvironment;
    private readonly IProductTagService _productTagService;
    private readonly CatalogSettings _catalogSettings;
    private readonly StoreInformationSettings _storeInformationSettings;
    private readonly CommonSettings _commonSettings;
    private readonly BlogSettings _blogSettings;
    private readonly NewsSettings _newsSettings;
    private readonly ForumSettings _forumSettings;
    private readonly LocalizationSettings _localizationSettings;
    private readonly CaptchaSettings _captchaSettings;
    private readonly VendorSettings _vendorSettings;
    private readonly IUrlRecordService _urlRecordService;
    private readonly ISeoService _seoService;
    private readonly IStoreService _storeService;
    private readonly ISettingService _settingService;

    #endregion

    #region Ctor

    public RobotFactory(ICategoryService categoryService, IProductService productService, IManufacturerService manufacturerService,
        ITopicService topicService, ILanguageService languageService, ICurrencyService currencyService,
        ILocalizationService localizationService, IWorkContext workContext, IStoreContext storeContext,
        ISitemapGenerator sitemapGenerator, IThemeContext themeContext, IThemeProvider themeProvider, IForumService forumService,
        IGenericAttributeService genericAttributeService, IWebHelper webHelper, IPermissionService permissionService,
        IStaticCacheManager cacheManager, IPageHeadBuilder pageHeadBuilder, IPictureService pictureService,
        IHostingEnvironment hostingEnvironment, CatalogSettings catalogSettings, StoreInformationSettings storeInformationSettings,
        CommonSettings commonSettings, BlogSettings blogSettings, NewsSettings newsSettings, ForumSettings forumSettings,
        LocalizationSettings localizationSettings, CaptchaSettings captchaSettings, VendorSettings vendorSettings,
        IProductTagService productTagService, IUrlRecordService urlRecordService, ISeoService seoService,
        IStoreService storeService, ISettingService settingService)
        : base(categoryService, productService, manufacturerService, topicService, languageService, currencyService,
              localizationService, workContext, storeContext, sitemapGenerator, themeContext, themeProvider, forumService,
              genericAttributeService, webHelper, permissionService, cacheManager, pageHeadBuilder, pictureService,
              hostingEnvironment, catalogSettings, storeInformationSettings, commonSettings, blogSettings, newsSettings,
              forumSettings, localizationSettings, captchaSettings, vendorSettings, productTagService)
    {
        this._categoryService = categoryService;
        this._productService = productService;
        this._manufacturerService = manufacturerService;
        this._topicService = topicService;
        this._languageService = languageService;
        this._currencyService = currencyService;
        this._localizationService = localizationService;
        this._workContext = workContext;
        this._storeContext = storeContext;
        this._sitemapGenerator = sitemapGenerator;
        this._themeContext = themeContext;
        this._themeProvider = themeProvider;
        this._forumservice = forumService;
        this._genericAttributeService = genericAttributeService;
        this._webHelper = webHelper;
        this._permissionService = permissionService;
        this._cacheManager = cacheManager;
        this._pageHeadBuilder = pageHeadBuilder;
        this._pictureService = pictureService;
        this._hostingEnvironment = hostingEnvironment;
        this._catalogSettings = catalogSettings;
        this._storeInformationSettings = storeInformationSettings;
        this._commonSettings = commonSettings;
        this._blogSettings = blogSettings;
        this._newsSettings = newsSettings;
        this._forumSettings = forumSettings;
        this._localizationSettings = localizationSettings;
        this._captchaSettings = captchaSettings;
        this._vendorSettings = vendorSettings;
        this._productTagService = productTagService;
        this._urlRecordService = urlRecordService;
        this._seoService = seoService;
        this._storeService = storeService;
        this._settingService = settingService;
    }

    #endregion

    #region Methods

    /// <summary>
    /// Get robots.txt file
    /// </summary>
    /// <returns>Robots.txt file as string</returns>
    public override string PrepareRobotsTextFile()
    {
        var storeScope = _storeContext.CurrentStore.Id;
        var seoSettings = _settingService.LoadSetting<SEOPluginSettings>(storeScope);

        var sb = new StringBuilder();

        //if robots.custom.txt exists, let's use it instead of hard-coded data below
        var robotsFilePath = System.IO.Path.Combine(CommonHelper.MapPath("~/"), "robots.custom.txt");
        if (System.IO.File.Exists(robotsFilePath))
        {
            //the robots.txt file exists
            var robotsFileContent = System.IO.File.ReadAllText(robotsFilePath);
            sb.Append(robotsFileContent);
        }
        else
        {
            //doesn't exist. Let's generate it (default behavior)
            var disallowPaths = new List<string>
            {
                // write your code here
            };
            var localizableDisallowPaths = new List<string>
            {
                // write your code here
            };

            // write your code here
        }

        return sb.ToString();
    }

    #endregion
}

现在您已经在DependencyRegistrar.cs文件中注册了服务

public class DependencyRegistrar : IDependencyRegistrar, INopStartup
{
    private const string CONTEXT_NAME = "nop_object_context__view_robot";

    public void Register(ContainerBuilder builder, ITypeFinder typeFinder, NopConfig config)
    {

        builder.RegisterType<RobotFactory>().As<ICommonModelFactory>().InstancePerLifetimeScope();
    }

    public void ConfigureServices(IServiceCollection services, IConfigurationRoot configuration)
    {
        services.Configure<RazorViewEngineOptions>(options =>
        {
            options.ViewLocationExpanders.Add(new CustomViewLocationExpander());
        });
    }

    public void Configure(IApplicationBuilder application)
    {
    }

    public int Order
    {
        get { return 100; }
    }
}

这将覆盖robot.txt文件的内容。

答案 1 :(得分:0)

您需要将名为“ robots.custom.txt” 的纯文本文件放置到网站的根目录中(对于 4.0 或更高版本,不是 wwwroot 目录),其内容将显示为robots.txt。