Tạo file robots TXT trong ASP.NET Core

 9/1/2019 |  Admin   3335 lượt xem

(netcore.vn) - Sau đây, .NET Core VN giới thiệu cách tạo file robots TXT trong ASP.NET Core và quản lý nó trong Admin Page.

Trong bài này, mình sẽ chia sẻ cho các bạn cách mình quản lý file robots.txt trong ASP.NET Core, đọc file robots.txt cho web.

Trước tiên, mình xin giới thiệu về file robots.txt trong web. Robots.txt đơn giản là 1 file txt giúp cho những con bot của công cụ tìm kiếm như Google có thể dễ dàng index dữ liệu của website mình hơn và người quản trị có thể quản lý những phần cho bot index hoặc không index từ những syntax được khai báo trong file. Nó liên quan nhiều đến SEO nếu anh em nào có tìm hiểu.

Tiếp theo, mình sẽ tạo 1 file robots.txt ở ngoài thư mục source .NET Core Việt Nam của mình. Sau đó mình thêm 1 function để khai báo về file robots để có thể hiện thị trên URL như ví dụ domain.com/robots.txt.

[HttpGet("robots.txt")]
        public IActionResult Robots()
        {
            Response.ContentType = "text/plain";
            var path = System.IO.Path.Combine(_appEnvironment.ContentRootPath, $"robots.txt");

            var result = new StringBuilder();

            string[] lines = System.IO.File.ReadAllLines(path);

            foreach (string line in lines)
            {
                result.Append(line);
                result.Append(Environment.NewLine);
            }
            ViewBag.Robots = result.ToString();
            return View();
        }

Sao đó mình tạo 1 Razor view như bên dưới, để đọc nội dung file robots ra ngoài view với không có layout kế thừa.

@{
    Layout = null;
}
@Html.Raw(ViewBag.Robots)

Vậy là mình đã định nghĩa xong 1 page robots cần thiết.

Nhưng mình muốn quản lý luôn file robots đó trong phần Admin để cho tiện lợi hơn thông qua thư viện System.IO để đọc và ghi file. Mình sử dụng API để làm nhé.

#region Robots
        [HttpGet("robots")]
        public IActionResult ReadFileRobots()
        {
            var path = System.IO.Path.Combine(_appEnvironment.ContentRootPath, $"robots.txt");

            var result = new StringBuilder();

            string[] lines = IO.File.ReadAllLines(path);

            foreach (string line in lines)
            {
                result.Append(line);
                result.Append(Environment.NewLine);
            }

            _logger.LogInformation(nameof(ReadFileRobots), path);

            return Ok(result.ToString());
        }

        [HttpPost("robots")]
        public IActionResult WriteFileRobots([FromBody] RobotsModel model)
        {
            var path = System.IO.Path.Combine(_appEnvironment.ContentRootPath, $"robots.txt");

            var response = new ResponseResult();

            if (model == null || string.IsNullOrEmpty(model.Text))
            {
                ModelState.AddModelError("Text", $"File robots rỗng!");
                var fails = FailValidations(ModelState, false, "Invalid!");
                return BadRequest(JsonResult(fails));
            }

            var file = IO.Path.Combine(path);

            if (!IO.File.Exists(file))
            {
                ModelState.AddModelError("Text", $"{file} không tìm thấy!");
                var fails = FailValidations(ModelState, false, "Invalid!");
                return BadRequest(fails);
            }

            IO.File.WriteAllText(file, model.Text);

            response.Status = true;
            response.Message = "Write File Robots Successfully!";
            _logger.LogInformation(nameof(WriteFileRobots), path);

            return Ok(response);
        }
        #endregion

Sau đó mình trả lên UI trong Admin Page sử dụng Html và Vue JS.

Anh em xem tiếp về ASP.NET Core nâng cao nhé nếu thấy hay.

liên quan

Tặng product key của Visual Studio 2022  13377

 4/29/2022

Tặng Product Key Của Visual Studio 2022 | Enterprise & Professional

Xem chi tiết 

Import file excel trong ASP.NET Core  3088

 4/21/2022

Import file excel trong ASP.NET Core

Xem chi tiết 

Roadmap cho ASP.NET Core Developer 2022  2345

 4/3/2022

Roadmap cho ASP.NET Core Developer

Xem chi tiết 

Hướng dẫn deploy lên IIS trong ASP.NET Core  5652

 3/26/2022

Hướng dẫn deploy lên IIS trong ASP.NET Core

Xem chi tiết 

Tạo project với .NET 5 và Clean Architecture từ A đến Z  1315

 1/25/2022

Tạo proejct với .NET 5 và Clean Architecture từ A đến Z

Xem chi tiết 

Tạo JWT Token với RSA private/public key trong .NET Core  1686

 12/24/2021

Tạo JWT Token với RSA private/public key trong .NET Core

Xem chi tiết 

Sử dung Google Translate APIs trong NET Core  1405

 6/25/2021

Sử dung Google Translate APIs trong NET Core

Xem chi tiết 

Tạo và gửi mẫu E-mail cho marketing trong ASP.NET Core  1307

 6/16/2021

Tạo và gửi mẫu E-mail cho marketing trong ASP.NET Core

Xem chi tiết 

Tạo trang quên mật khẩu trong ASP.NET Core  1766

 6/15/2021

Tạo trang quên mật khẩu trong ASP.NET Core

Xem chi tiết 

Chèn watermark vào hình ảnh upload trong ASP.NET Core  1311

 5/26/2021

Chèn watermark vào hình ảnh upload trong ASP.NET Core

Xem chi tiết 
Like Fanpage Để Ủng Hộ Chúng Tôi Duy Trì Website