Get web api appSettings as collection of name-value pairs - asp.net-web-api

Is there a way to retrieve the values inside the 'appSettings' section of my web.config file as a collection of name-value pairs? I want to create an api controller method that returns this object so that I can access the collection of settings from my front-end(as a JSON object ideally). I tried returning WebConfigurationManager.AppSettings but it only returns an array of string containing only the name of the settings (without the value). I had been searching for an answer for almost 2 hours but couldn't find one. Any help? Thanks.

There is no simple mechanism provided for this that I am aware of, for the reason that the NameValueCollection type is more than a simple Name/Value dictionary and might not always be easily represented as one.
Each key in a NameValueCollection may have more than one value.
When more than one value is assigned to a particular Name then the Value of that Name becomes a comma separated list of all of those values. If any one of those values itself contains a comma then this can make deciphering such values difficult (i.e. application/context specific).
As a result, you have to decide how you need to handle the contents of a specific NameValueCollection in each case.
If your particular AppSettings consist of truly simple name/value pairs or you are happy to deal with any comma separated multi-values then you can of course do something similar to:
var dict = new System.Collections.Generic.Dictionary<String, String>();
var settings = System.Configuration.ConfigurationManager.AppSettings;
foreach ( var k in settings.AllKeys )
{
dict.Add(k, settings[k]);
}
var json = Newtonsoft.Json.JsonConvert.SerializeObject(dict);
Hopefully that will set you down the right track if not provide a complete solution.

Related

Dialogflow CX - using sys.person gives wierd output

https://i.stack.imgur.com/k1bZD.png
I am using sys.person to capture the person's name. Is there any way for just 'john' and not the entire {"name": "john"} to show instead? I want to avoid sys.given-name and sys.last-name since dialogflow says they are deprecated.
Do you have your entity configured as a list? If it is a list, you probably have to output the parameter like $session.params.name[0].name.
If it's not a list, $session.params.name.name would work, but in that case I suggest you change the entity name to person, as it would be more readable to see $session.params.person.name
From your screenshot, it looks like your parameter is an isList parameter.
I have checked today the behavior of static responses when referencing isList parameters and it seems that we can now display the list of values for a certain parameter without the array index in the responses using the format: $session.params.parameter-id.
In your case, it would be:
$session.params.name.
In addition, if you want to reference a specific array index in an isList parameter, you can use the format $session.params.parameter-id[i]. In your case, it would be $session.params.name[0]
or if you want to get the original value you can reference it as $session.params.name[0].original

Ruby Find a value in a complex JSON object

I need to find the value of a particular key (say "key") in a JSON object. Since I am getting the JSON object dynamically, I do not know the structure of the JSON object.
Could anyone tel me an easy way to find the value of a key in such a JSON object?
If you dont know the structure of the JSON then you will need to do more work to inspect all possible keys.
You might just have to do a recursive descent and iterate over all keys until you find the key whose name matches key and then grab its value.

How can I use a list function in CouchDB to generate a valid (/normal) ViewResults object?

I have a simple problem I need to solve, and list functions are my current attempt to do so. I have a view that generates almost what I need, but in certain cases there are duplicate entries that make it through when I send in edge-case parameters.
Therefore, I am looking to filter these extra results out. I have found examples of filtering, which I am using (see this SO post). However, rather than generate HTML or XML or what-have-you, I just want a regular ol' view result. That is, the same kind of object that I would get if I queried CouchDB without a list function. It should have JSON data as normal and be the same in every way, except that it is missing duplicate results.
Any help on this would be appreciated! I have tried to send() data in quite a few different ways, but I usually get that "No JSON object could be decoded", or that indices need to be integers and not strings. I even tried to use the list to store every row until the end and send the entire list object back at once.
Example code (this is using an example from this page to send data:
function(head, req) {
var row; var dupes = [];
while(row=getRow()) {
if (dupes.indexOf(row.key) == -1) {
dupes.push(row.key);
send(row.value);
}
};
}
Lastly, I'm using Flask with Flask-CouchDB, and I'm seeing the aforementioned errors in the flask development server that I'm running.
Thanks! I can try to supply more details if need be.
Don't you need to prepend a [, send a , after each row value except the last, and end with ]? To actually mimic a view result, you'd actually need to wrap that in a JSON structure:
{"total_rows":0,"offset":0,"rows":[<your stuff here>]}

ASP.NET Web API - GET request with multiple arguments

What I'm trying to do is to have an api request like /api/calculator?1=7.00&2=9.99&3=5.50&4=45.76 etc. How my controller can grab the request data? The keys/codes of the query string are ints between 1 and 1000. In the query string they can be some of the 1000 codes, not necessarily all of them. The value part is doubles.
The one way I think would work is if I create a model object (ex StupidObject) with 1000 properties (should use properties named like p1, p2,..p1000 for codes now, as ints are not an allowed property name), decorated with ModelBinder. Then for the controller I could have something like GetCalcResult(StupidObject obj){...} But that doesn't seem like an elegant solution :)
I tried controllers like GetCalcResult([FromURI]Dictionary<int, double> dict){...} but dict is always null. Also without the [FromURI] I get an error. Also tried List<KeyValuePair<int, double>> as controller parameter with the same results.
Could anyone point me at the right direction or give me an working example?
One way is to avoid trying to pass the values in as a parameter and simply do
var queryValues = Request.RequestUri.ParseQueryString();
in your action method. QueryValues will be NameValueCollection that you can iterate over to get access to your query parameters.
If you really want to use a parameter, having a parameter of type [FromUri]FormDataCollection might work.

What to call a method that finds or creates records in db

This question might seem stupid but i nonetheless i believe it's a worth asking questioin.
I work on some web application when we use tags which are attached to articles.
When adding new article users provides a list of tags he wish to associate with his new article.
So when the form is submitted and my MVC controller processes request i have a string with tags from the form. I split the tags string and i get an array of tag words. Then i need a
list o tags ids from db. So i came up with a method that takes a list of tag words and checks if each tag already exists in db. If given tag is already in db then tag's id is appended to result array. If tag does not exist in db it is created and then id of just created tag is appended to result array.
My question is: what is the best name for such a method? This method creates tags only if necessary and returns list of tags' ids.
I tried this names, but none of them looks right to me:
fetchTagsIds(List tagWordsList)
createOrFindsTagsIds(List tagWordsList)
obtainTagsIds(List tagWordsList)
I need a name that really reflects what the method does. Thanks for your help :)
I'd drop the "s" in "Tags". So that the method is:
FetchTagIds(List tagWordsList)
IdsOfTags. That it creates them is an implementation detail (as is the fact that it uses a relational database in the first place). All that matters is that it gives you unique ids for tags (e.g. it could also hash them with a perfect hash function, or lookup an ID in the web.
If you're having trouble coming up with a name that accurately describes what your method does, that could be because you method does too much. Perhaps it would be better to refactor it into two:
findTagsByName(List tagNames) would return a list of Tag objects based on list of names you pass in
persistTags(List tags) - or saveTags(), or createTags() - would persist a list of Tag objects.
getTagIDs?
Get suggests that the code will do something to get an ID when one doesn't exist to fetch.

Resources