How to avoid hard coding URLS

Viewed 33

I'm hosting a .NET web api using IIS, the api features a password reset function which send the user to a reset password page, now im wondering how would could I avoid hardcoding the url

 private ReturnHandler SendResetPasswordMail(string email, string ott)
    {
        ReturnHandler result = new ReturnHandler();
        List<string> emails = new List<string>();
        emails.Add(email);
        string subject = "Promjena lozinke";
        string link = "http://axatszg-vw0038:13702/#/change-password";
        string[] parameters = { subject, "link za promjenu lozinke:", link + "?token="  + ott};

basically instead of fixating "axatszg-vw0038:13702" or "localhost:13702" could I somehow have my code access the host

1 Answers

In Web.config you can put your value in appSettings, like:

<configuration>
    <appSettings>
        <add key="url" value="yourURL" />
    </appSettings>
</configuration>

Then you can get it like:

string link=System.Configuration.ConfigurationManager.AppSettings["url"];
Related