0

ASP.Net MVC Web app - I have the following code in my Web.config file:

<configuration> <configSections> <!-- For more information on Entity Framework configuration, visit http://www.contoso.com --> <section name="entityFramework" type="words, EntityFramework, Version=numbers, Culture=more words" /> </configSections> <appSettings> <add key="x-coord" value="x1,x2,x3" /> <add key="y-coord" value="y1,y2,y3" /> </appSettings> </configuration> 

And this is a conditional inside the method that uses the <appSettings> key-values:

if (ModelState.IsValid && ((ConfigurationManager.AppSettings["x-coord"].Contains(postModel.xCoordinate.ToLower()) & ConfigurationManager.AppSettings["y-coord"].Contains(postModel.yCoordinate.ToLower)))) { return View(postModel); } 

The issue is that as this code is written, as long as any of the x-coord values are passed, and any of the y-coord values are passed, the conditional will evaluate to true.

I need to have the code function such that x1 and y1 must be passed to evaluate to true, OR, x2 and y2, OR, x3 and y3, such that these are true key-value pairs, as opposed to just two Lists.

I believe this all comes down to the Contains method, which is what is allowing any pair of x and y to be allowed. I just don't know how to go about enforcing strict pairing as outlined above.

1
  • a) Use && rather than &. b) Strip the first character off the x and y co-ordinate and compare them. c) Don't use Contains like you are - x will be found even though it has no number. String.Split the config setting into an array and then run Contains / IndexOf on that. Commented Jun 12, 2019 at 23:42

1 Answer 1

1

You seem to be confused about what your code is doing here. When you set <add key="x-coord" value="x1,x2,x3" /> in your web.config you're adding a key with a single value which is a string of "x1,x2,x3" so when you call ConfigurationManager.AppSettings["x-coord"].Contains(postModel.xCoordinate.ToLower()) all you're doing is checking if the x-coord string contains whatever is in postModel.xCoordinate (converted to lowercase obviously). You then do an entirely separate check on your 'y-coord` value. There is no relationship between the two.

I'm not a fan of using your web.config for holding application logic like this, but if this is really the way you want to go, I'd set it up so you can split the string and process coords separately. So something like:

<add key="coords" value="x1:y1,x2:y2,x3:y3" />

And then you can do something like:

 var coords = ConfigurationManager.AppSettings["x-coord"].Split(',').ToList(); coords.ForEach(c => { var coord = c.Split(':'); if (coord[0].Equals(postModel.xCoordinate.ToLower()) && coord[1].Equals(postModel.yCoordinate.ToLower())) { return View(postModel); } } ); 
Sign up to request clarification or add additional context in comments.

6 Comments

Hi Ben, thanks for this I do believe it gets me close to the answer, the only issue is I'm having trouble getting it to work in the context of my Model. I realize I only posted one block of code in my question, but I'm having a heck of time getting it to work inside the method. Basically it seems the issue is that I have to have the return View(postModel); outside of the code you've provided - but once it's there, it's not longer part of your IF statement. I want to mark you answer as answer but just need to get it working first.
@Stpete111 If I understand the issue, what I'd do is set a bool found = false; before the foreach loop, and set to true if you get a match (where my code currently returns the view). Then you can use that boolean value where your actual code does it's return to return whatevers appropriate.
Aha, great idea. Let me try that and I'll report back asap.
Ben, I tried your suggestion, and this definitely gets me closer, but something's still off: I'm debugging right now and c[0] and c[1] are not evaluating to any value, and therefore found remains as false. Coords values are correct during debug, as are xCoordinate and yCoordinate. So something isn't working with that Split method. Are you pretty confident on that syntax? Thanks again for your help.
Oh, no, typo. I'll fix it, but it should be checking coord[0] and coord[1] since that's the array the coordinate should have been split into.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.