I have an asp.net core mvc project, I have an IActionResult in my controller that…
- User
I have an asp.net core mvc project, I have an IActionResult in my controller that creates a service, That falls under a url like this: "http://elctronicads.com/Home/Service/Service_Id". I wanna make each service appear on google search on it's own. So i need you to write a code in the controller that adds said url to my sitemap.xml that is located inside wwwroot file. this is the sitemap.xml: " http://elctronicads.com/ 2023-06-08 monthly 1.0 http://elctronicads.com/Home/Services 2023-06-08 monthly 0.8 http://elctronicads.com/مقاولات-الكويت 2021-10-13 monthly 0.8 ", need the code in the controller to add another for each service added.
- Assistant
You can do this by loading the sitemap.xml file, editing it, and saving it back. Here's a code snippet to perform this operation:
using System; using System.IO; using System.Xml.Linq; public IActionResult AddServiceToSitemap(int serviceId) { string sitemapPath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "sitemap.xml"); XDocument sitemap = XDocument.Load(sitemapPath); XElement newUrlElement = new XElement("url", new XElement("loc", $"http://elctronicads.com/Home/Service/{serviceId}"), new XElement("lastmod", DateTime.UtcNow.ToString("yyyy-MM-dd")), new XElement("changefreq", "monthly"), new XElement("priority", "0.8") ); sitemap.Root.Add(newUrlElement); sitemap.Save(sitemapPath); // Redirect to the appropriate action or return a view return RedirectToAction("Index"); }Replace the AddServiceToSitemap method's content with your actual IActionResult method, and replace the
serviceIdvariable with the actual ID value. This code will load the sitemap.xml file, create a new<url>element, append it to the existing XML, and then save the file back to disk. Finally, it will redirect the user to the "Index" action or the view you specify. - User
My database is saving the record Id by identity, So how can i get the record i just created and use it to add said record to the .xml file without getting "instance not set to an instance of an object" error? " [HttpPost] [ValidateAntiForgeryToken] public async Task ClientAdd(string name, string description, string phones, IFormFile img, string keywords) {
var ad = new Client { Name = name, Description = description, Phones = phones, Keywords = keywords }; if (img != null && img.Length > 0) { using (var memoryStream = new MemoryStream()) { await img.CopyToAsync(memoryStream); ad.Avatarimg = memoryStream.ToArray(); } } _context.Clients.Add(ad); await _context.SaveChangesAsync(); string sitemapPath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "sitemap.xml"); XDocument sitemap = XDocument.Load(sitemapPath); XElement newUrlElement = new XElement("url", new XElement("loc", $"http://elctronicads.com/Home/Company/{ad.Id}"), new XElement("lastmod", DateTime.UtcNow.ToString("yyyy - MM - dd")), new XElement("changefreq", "monthly"), new XElement("priority", "0.8") ); sitemap.Root.Add(newUrlElement); sitemap.Save(sitemapPath); return RedirectToAction("Clients", new { Controller = "Admin", Action = "Clients"}); }" - Assistant
Your code seems correct, and it should work without giving a "null object" error. However, you can add a condition to check if the object exists before performing the sitemap update:
using System; using System.IO; using System.Xml.Linq; [HttpPost] [ValidateAntiForgeryToken] public async Task<IActionResult> ClientAdd(string name, string description, string phones, IFormFile img, string keywords) { var ad = new Client { Name = name, Description = description, Phones = phones, Keywords = keywords }; if (img != null && img.Length > 0) { using (var memoryStream = new MemoryStream()) { await img.CopyToAsync(memoryStream); ad.Avatarimg = memoryStream.ToArray(); } } _context.Clients.Add(ad); await _context.SaveChangesAsync(); if (ad != null && ad.Id > 0) // Add this condition to check if the object exists and has a valid Id { string sitemapPath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "sitemap.xml"); XDocument sitemap = XDocument.Load(sitemapPath); XElement newUrlElement = new XElement("url", new XElement("loc", $"http://elctronicads.com/Home/Company/{ad.Id}"), new XElement("lastmod", DateTime.UtcNow.ToString("yyyy-MM-dd")), new XElement("changefreq", "monthly"), new XElement("priority", "0.8") ); sitemap.Root.Add(newUrlElement); sitemap.Save(sitemapPath); } return RedirectToAction("Clients", new { Controller = "Admin", Action = "Clients" }); }By checking if
adis null and has a validIdwithif (ad != null && ad.Id > 0), you're ensuring that the ad is properly saved and has a valid identifier before updating the sitemap file.