Ice_
Ice_
CC#
Created by Ice_ on 3/27/2024 in #help
✅ Progressbar progression over taskbar icon without nuget
I'm trying to find a way to do this with Windows API or something else. I don't want to use nuget packages for this. I've googled around but I don't seem to find any recent information about it, only really old threads. Any ideas?
5 replies
CC#
Created by Ice_ on 12/21/2023 in #help
Dependency injection in WPF
I'm trying to do DI in my WPF app. I've read about it on different sites and followed youtubers but it doesn't seem to work (it runs but I can't reach the registered service, it'll always be null). I am basically trying to inject a DbContext as a Singleton because its scoped nature is causing some issues in the application (entity mismatching etc. = badly written code probably). Help/Tips please!
18 replies
CC#
Created by Ice_ on 12/20/2023 in #help
WPF Checkbox handling
I have a class(Owner) which has a property list of a second class(Pet) and vice versa (many to many relationship). It is all connected with a dbcontext. I am supposed to check checkboxes of pets that a specific owner has. This works and it's storing everything in the database. The problem I have is when I select an owner, I want to iterate through all the pets for that particular owner and automatically check all the checkboxes for those pets in the list showing. If I check/uncheck a box through code, the IsChecked on each checkbox (which is bound to a bool property) is triggered and it sets the bool to true or false depending on the currently value (if checked = uncheck and the other way around). This in turn also triggers an add or remove to the list/database which is not what I want since the user never clicked the checkboxes. I know there is Checked and Unchecked events for checkboxes but I'm supposed to leave the MainWindow.xaml file as empty as possible as it's a part of my assignment. When using Command it seems like you can't add parameters either. Any tips/ideas? Going to try to tackle this tomorrow with a friend!
9 replies
CC#
Created by Ice_ on 10/17/2023 in #help
❔ Deploying an MVC project
I know this is a C# server but it's related so I hope it's okay... I've published a standard MVC project to a folder and then added that folder with settings in IIS manager. It won't work. I only get error 500 when trying to access the site. So I launched the built project with --urls "https://ipnumber:443" and then went into my router and ip forwarded everything and it works just fine except for the fishy certificates of course. My question is, why wont IIS work (everything is by default)? I would also want to add a signed certificate some day, how would I do that outside of the IIS manager so it just magically works by running the executable?
2 replies
CC#
Created by Ice_ on 5/11/2023 in #help
❔ Creating a JWT through terminal and use it in production (webapi)
I've created a JWT through the terminal with "dotnet user-jwts create" inside the webapi source directory. It returns a token I've stored. I have added [Authorize] to a specific endpoint and it works during debug if I use the correct token (Postman, POST), otherwise it will return a 401. Now, if I compile the project for "Release" and then run the webapi from its directory it will launch the application just fine and listen on all the ports I'd like. However, when trying to use the endpoint using [Authorize] from Postman, it'll give me a 401. The WWW-Athenticate will say "Bearer error="invalid_token", error_description="The signature key was not found"". Adding additional log levels in the actual webapi gives me this: info: Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerHandler[1] Failed to validate the token. Microsoft.IdentityModel.Tokens.SecurityTokenSignatureKeyNotFoundException: IDX10500: Signature validation failed. No security keys were provided to validate the signature. at System.IdentityModel.Tokens.Jwt.JwtSecurityTokenHandler.ValidateSignature(String token, TokenValidationParameters validationParameters, BaseConfiguration configuration) at System.IdentityModel.Tokens.Jwt.JwtSecurityTokenHandler.ValidateJWS(String token, TokenValidationParameters validationParameters, BaseConfiguration currentConfiguration, SecurityToken& signatureValidatedToken, ExceptionDispatchInfo& exceptionThrown) Summary: It works when running the application in Visual Studio (both Debug and Release) but it does not work with JWT when running the executable in the Release directory outside of Visual Studio. I do know that the JWT is stored in %appdata%\Microsoft\UserSecrets when using dotnet user-jwts create. Do I somehow need to point the Release-version to that path or what am I missing? And yeah, this is very basic JWT stuff. We are just scraping the surface in my education so there's a lot more that the school doesn't go into details about 🙄
17 replies
CC#
Created by Ice_ on 5/10/2023 in #help
✅ Building a web api release. No HTTPS and how do I change ports?
I am trying to do a release build for a small web api I've made. I can run it but it defaults on port 5000 and it's using HTTP. I eventually get an error message when I try to use a POST on an API endpoint that is protected by a JWT. I get a http error 401 (unauthorized) and I get this in the console window: Microsoft.AspNetCore.HttpsPolicy.HttpsRedirectionMiddleware[3] Failed to determine the https port for redirect. I suspect the 401 error is because it's being sent over HTTP and not HTTPS (getting this idea from a couple of threads after googling). I've googled a lot and all the results comes down to launchSettings.json under properties or appsettings.json but none of them even have the HTTP port 5000 in them. A few forums suggested go into project settings and go into the web tab and change the information there but there is no such tab for me. I'm not really sure how to progress here. Help!
89 replies
CC#
Created by Ice_ on 5/8/2023 in #help
❔ LINQ - I get confused by Where, Find, FindFirstValue etc.
I don't know what method works with certain Types. If I make an array of string and use .Find it doesn't exist. If I make a List of string .Find will be there. I understand that .Find is for lists but it's still confusing and there are other methods than .Find. Is there some sort of cheat sheet somewhere that makes all this clearer?
8 replies
CC#
Created by Ice_ on 5/5/2023 in #help
❔ Blazor Server - scaffolding Identity error
It keeps giving me the error message: Error There was an error running the selected code generator: 'Length cannot be less than zero. (Parameter 'length')' Identity is working and I can register/login, I just can't scaffold in the files for login/register so I can update the HTML. Googling it seems to suggest this is a common problem 🤔 What could be the problem?
5 replies
CC#
Created by Ice_ on 4/17/2023 in #help
❔ Returning the user object that was just created after web api [POST]
I have a web API and the POST works just fine in Swagger. It does work fine if I use the web API from my website application but when I do a PostAsJsonAsync against api/User, I won't get the object back (so I can retrieve the new UserId). I guess this is intended. How would I make the POST method in the UserController to return the object back? A simple "return userobject;" wont suffice!
10 replies
CC#
Created by Ice_ on 4/13/2023 in #help
❔ Bad request (400) - web api
I am not sure why this is happening because it works splendid with one class and not with another class for whatever reason. The code is basically identical in the controllers (and repositories). I don't have the code to show you because it's code from a friend of mine earlier today through Zoom. What we discovered was is that the POST method does not execute in the controller at all. The 400 error message is thrown before it actually connects to the API. It works through Swagger though, so there's an issue with the httpclient I guess but I can't really seem to pinpoint what the issue is. We are using PostAsJsonAsync in this case. My question for this whole thread is: Can I somehow print out the details of the "bad request" error code? What part is "bad"? The error codes gives you a good pointer but when you really can't see what the hell is going wrong, you want the details. So how do I get them? 😅
5 replies
CC#
Created by Ice_ on 3/16/2023 in #help
❔ Blazor InputSelect, default value is null
I have a Blazor project and I am using an InputSelect and inside it a foreach loop to populate the list with fruit categories. When selecting one of the categories, I have another foreach loop that populates the fruits in the selected category. I then I have a button on this form to send an "order" with the fruitId. It works but I noticed that if I am happy with the first item in the list and don't actively choose the fruit and just press order, the value from the <option> will be null/0. So it seems the first item has no value. Until I change the fruit and then change it back to the first fruit. Then when ordering, I will get the fruitId. For now I've "fixed it" by adding an <option selected value="">Select fruit...<option>. Since I use EditForm with model binding, it will complain that, that particular option isn't valid if the user is trying to order. However, if the user selects a fruit category and just press order ("Select fruit..." will be default), the form will accept the data. So I guess I have two issues but if I somehow can fix the first issue, I won't need to add an extra option so the user is "forced" to actively choose a fruit. Any ideas how to fix this?
3 replies
CC#
Created by Ice_ on 3/12/2023 in #help
❔ Entity Framework. Rental list
I have a class for books and I have a class of customers. I also have a class for loans. The books should be possible to be loaned by other customers as well but only after the loaner has given it back and the loaner can choose themselves how many days they want to loan the book, a maximum of a week though. The loans class should be a junction table, I assume this is a many-to-many relation? I can figure out the relation-stuff in EF but I'm unsure how to check the timespan between date1 and date2. It's of course of type DateTime. It seems like I can use calculus to calculate the datetimes but how would this look like with either Linq or with a lambda expression?
20 replies
CC#
Created by Ice_ on 3/9/2023 in #help
❔ Blazor sub-component PageTitle overrides parent component?
Why? Tried some stuff but it wont work.
2 replies
CC#
Created by Ice_ on 3/9/2023 in #help
❔ Filter list with lambda expression?
Hello! Right now I have a "MyList = blahaRepo.GetAllUsers();" which retrieves all users. Instead of creating a separate method for retrieving users with a specific flag or creating a new list and run a foreach and add those with the specific flag to the new list - Can I simply use a lambda expression along with the line above to filter out while filling the first list? Or is there a better way to filter out the data?
37 replies
CC#
Created by Ice_ on 1/23/2023 in #help
❔ ASP.net MVC repositories
I have three classes, each with their own interface and repository. Let's call the classes Lamp, Sockets and the third class is a basic "joining" class called LampSockets where you just add LampId and SocketId to the table. It's not the best way to join two classes but that was our way of doing things during this assignment. My question is, what's the best way to optimize the code? Should I only use the repositories related to the actual class or is it okay to use more than one repository in a class? Because a part of the assignment is to create something in the joining table without IDs, using a ViewBag with SelectList (which is done). But listing everything with names instead of Ids (in index) is troublesome when I can't use several repositories. How would I accomplish that? (I've not been disallowed to use several repositories but I'm just trying to follow the assignment criterias 😅)
5 replies
CC#
Created by Ice_ on 11/13/2022 in #help
❔ Entity Framework - paired tables. Can add but not remove from paired table?
I've setup a Student class and a Course class. I've added the properties and also added necessary dbset in the dbcontext class. I've added two students manually, one with id 1 and another with id 2 of course. Same goes with courses. (dbc = dbcontext) I then add a course to student 2 with: Student student = dbC.Students.First(s => s.StudentID == 2); Course course = dbC.Courses.First(s => s.CourseId == 2); student.Course.Add(course); This will add to the the CourseStudent table: CorseId: 2 | StudentStudentID: 2 (note: Why does it name the column to StudentStudentID and not the same with CorseId?) If I repeat the code above but change from Add to Remove, it does not remove it. I've added a breakpoint and if I hove above course variable it will say that "Students" is null. Where did things go wrong? 😅
4 replies
CC#
Created by Ice_ on 11/6/2022 in #help
❔ When to use an Interface? Example?
I'm not entirely sure what the difference between interface vs abstract class is. As far as I know interfaces has no properties - just methods? Got any good examples on that? I've tried googling a bit but I'm finding variating results!
19 replies
CC#
Created by Ice_ on 10/26/2022 in #help
Arrays in constructors and lists
I have made a constructor taking an array of int (int[] numbers) in a new class, let's call the class Digits. I have made a public property to an array of int in the class called num (with get; set;). I then make a new object in my main class and assign some numbers, three of them: Digits tmpobj = new(new int[] { 16,23,51 }); I then try to make a list: List<Digits> myList = new List<Digits>(); myList.Add(tmpobj); If I do a Console.Writeline(tmpobj.num[0]); it will return 16. If I increment it with one I get 23 etc. I then do a foreach (var item in myList). I've done a breakpoint after that and I see that myList in this case shows [0] .num[0] = 16, next 23 etc. Then I've done a Console.Writeline after each item and it's just returning the int32 stuff, I thought the "Add" method would add the full array to the list but it does not. I can see that stuff isn't right but I'm not sure how to fix this at the moment. Please help 😅
24 replies