hello hello - 9 days ago 6
ASP.NET (C#) Question

String Replace issues

I'm trying to replace all instances of a variable in text file using this function

public static string GetTextContent(string location, string callbackfromasync)
{
var markup = HttpContext.Current.Server.MapPath(location);
var correctString = markup.Replace("callbackUrl", callbackfromasync);
return File.ReadAllText(correctString);
}


I am calling the method like this:

await UserManager.SendEmailAsync(user.Id, "Confirm your account", GetTextContent(GetRegistrationEmailMarkUp, callbackUrl));


GetRegistrationEmailMarkUp
should return a string of HTML.

The markup I'm working with looks like this.

Why is the string not being replaced?

Answer

You've got the operations mixed up. You need to get the data from the file to modify it, not modify the path of the file:

string markupFile = HttpContext.Current.Server.MapPath(location);
string markupTemplate = File.ReadAllText(markupFile);
string modifiedHtml = markupTemplate.Replace("callbackUrl", callbackfromasync);
return modifiedHtml;