0
\$\begingroup\$

There is a bit of if-else-if going in within the code. Is there a construct which could come handy. Also the section names are very similar too

public string GetRestApiUrlFromHost()
{
 var restApiUrl = string.Empty;
 var HostUrl = Request.Host.Value;
 var DevURL = _configuration.GetSection("DEV_API_URL").Value;
 var QAURL = _configuration.GetSection("QA_API_URL").Value;
 var ProdURL = _configuration.GetSection("PROD_API_URL").Value;
 if (HostUrl.Contains(DevURL, StringComparison.CurrentCultureIgnoreCase))
 {
 restApiUrl = _configuration.GetSection("DEV_REST_API_URL").Value;
 }
 else if (HostUrl.Contains(QAURL, StringComparison.CurrentCultureIgnoreCase))
 {
 restApiUrl = _configuration.GetSection("QA_REST_API_URL").Value;
 }
 else if (HostUrl.Contains(ProdURL, StringComparison.CurrentCultureIgnoreCase))
 {
 restApiUrl = _configuration.GetSection("PROD_REST_API_URL").Value;
 }
 else
 {
 restApiUrl = _configuration.GetSection("Local_REST_API_URL").Value;
 }
 return restApiUrl;
}

Looks redundant to me. But cannot place it. Which would be the most ideal way to refactor this code?

200_success
145k22 gold badges190 silver badges478 bronze badges
asked Feb 2, 2022 at 15:42
\$\endgroup\$
4
  • \$\begingroup\$ Welcome to Code Review! Are you the author or maintainer of this code? \$\endgroup\$ Commented Feb 2, 2022 at 16:04
  • 1
    \$\begingroup\$ We need to know what the code is intended to achieve. To help reviewers give you better answers, please add sufficient context to your question, including a title that summarises the purpose of the code. We want to know why much more than how. The more you tell us about what your code is for, the easier it will be for reviewers to help you. The title needs an edit to simply state the task, rather than your concerns about the code. \$\endgroup\$ Commented Feb 2, 2022 at 16:45
  • 1
    \$\begingroup\$ @SᴀᴍOnᴇᴌᴀ: Yes I am a maintainer of the code. I didn't like the way its done. Was thinking of a Dictionary<string, string> approach. Got a little bit stuck up with work while adding the code. \$\endgroup\$ Commented Feb 2, 2022 at 17:32
  • \$\begingroup\$ @TobySpeight: Three environments for the project and three file servers for each. Was getting the file server base url. Thanks. Sorry that my question was not clear enough. First time user here.(Not at Stackoverflow though) \$\endgroup\$ Commented Feb 2, 2022 at 17:35

1 Answer 1

2
\$\begingroup\$

You could create an array of tuples with url keys and section keys and loop them like this:

(string urlKey, string sectionKey)[] keys = new [] {
 ("DEV_API_URL", "DEV_REST_API_URL"),
 ("QA_API_URL", "QA_REST_API_URL"),
 ("PROD_API_URL", "PROD_REST_API_URL")
};
string sectionKey = "Local_REST_API_URL";
foreach (var key in keys) {
 if (HostUrl.Contains(key.urlKey, StringComparison.CurrentCultureIgnoreCase)) {
 sectionKey = key.sectionKey;
 break;
 }
}
return _configuration.GetSection(sectionKey).Value;

Or with LINQ (with same tuple array). .NET 6:

var key = keys.FirstOrDefault(
 k => HostUrl.Contains(k.urlKey, StringComparison.CurrentCultureIgnoreCase),
 (null, "Local_REST_API_URL")
);
return _configuration.GetSection(key.sectionKey).Value;

Uses the FirstOrDefault(IEnumerable, Func<TSource,Boolean>, TSource) extension method overload available since .NET 6

Framework versions prior to .NET 6:

var key = keys
 .Where(
 k => HostUrl.Contains(k.urlKey, StringComparison.CurrentCultureIgnoreCase))
 .DefaultIfEmpty((null, "Local_REST_API_URL"))
 .First();
);
return _configuration.GetSection(key.sectionKey).Value;
answered Feb 2, 2022 at 16:08
\$\endgroup\$
1
  • \$\begingroup\$ Thanks. HostUrl.Contains(_configuration.GetSection(key.urlKey)... should be my case \$\endgroup\$ Commented Feb 2, 2022 at 17:34

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.