Cydo
Cydo
Explore posts from servers
CC#
Created by Cydo on 12/4/2024 in #help
How can I make FluentValidation return errors in camel case??
I have tried created an ValidationInterceptor and configuring my program.cs to return json in camel case but nothing seems to work
using FluentValidation;
using FluentValidation.Results;
using Microsoft.AspNetCore.Mvc;
using System.Linq;
using System.Text.Json;
using FluentValidation.AspNetCore;

namespace CollabParty.Application.Common.Validators
{
public class CamelCaseValidationInterceptor : IValidatorInterceptor
{
// Before MVC validation
public ValidationContext<T> BeforeMvcValidation<T>(ControllerContext controllerContext, ValidationContext<T> context)
{
return context; // No changes before validation
}

// After MVC validation: Convert the property names to camelCase
public ValidationResult AfterMvcValidation<T>(ControllerContext controllerContext, ValidationContext<T> context, ValidationResult result)
{
var camelCasedErrors = result.Errors.Select(error =>
new ValidationFailure(
JsonNamingPolicy.CamelCase.ConvertName(error.PropertyName), // Convert to camelCase
error.ErrorMessage,
error.AttemptedValue)
{
ErrorCode = error.ErrorCode,
CustomState = error.CustomState,
Severity = error.Severity
}).ToList();

return new ValidationResult(camelCasedErrors);
}

// These methods are not needed but can be left as no-ops
public IValidationContext BeforeAspNetValidation(ActionContext actionContext, IValidationContext commonContext)
{
return commonContext;
}

public ValidationResult AfterAspNetValidation(ActionContext actionContext, IValidationContext validationContext, ValidationResult result)
{
return result;
}
}
}
using FluentValidation;
using FluentValidation.Results;
using Microsoft.AspNetCore.Mvc;
using System.Linq;
using System.Text.Json;
using FluentValidation.AspNetCore;

namespace CollabParty.Application.Common.Validators
{
public class CamelCaseValidationInterceptor : IValidatorInterceptor
{
// Before MVC validation
public ValidationContext<T> BeforeMvcValidation<T>(ControllerContext controllerContext, ValidationContext<T> context)
{
return context; // No changes before validation
}

// After MVC validation: Convert the property names to camelCase
public ValidationResult AfterMvcValidation<T>(ControllerContext controllerContext, ValidationContext<T> context, ValidationResult result)
{
var camelCasedErrors = result.Errors.Select(error =>
new ValidationFailure(
JsonNamingPolicy.CamelCase.ConvertName(error.PropertyName), // Convert to camelCase
error.ErrorMessage,
error.AttemptedValue)
{
ErrorCode = error.ErrorCode,
CustomState = error.CustomState,
Severity = error.Severity
}).ToList();

return new ValidationResult(camelCasedErrors);
}

// These methods are not needed but can be left as no-ops
public IValidationContext BeforeAspNetValidation(ActionContext actionContext, IValidationContext commonContext)
{
return commonContext;
}

public ValidationResult AfterAspNetValidation(ActionContext actionContext, IValidationContext validationContext, ValidationResult result)
{
return result;
}
}
}
2 replies
CC#
Created by Cydo on 11/28/2024 in #help
✅ Setup SendGrid to send emails, but not getting them
No description
3 replies
CC#
Created by Cydo on 11/27/2024 in #help
✅ How can I take 2 existing repos (client and backend) and combine them?
I've always made my frontend and backends separate repos, and would like to instead make set it up where the frontend and client are both in the same repo. But it doesnt seem as simple as just creating a parent folder with 2 subfolders one for frontend and one for backend and then or am I wrong? I assumed you'd have one .gitignore in the root instead of a .gitignore in each subfolder? Or because how .net has solutions is it more complex?
39 replies
CC#
Created by Cydo on 11/23/2024 in #help
✅ JTI not being pulled from payload when I am trying to validate token
I create an access token store all the claims i want on it and get an access token and this is what the payload looks like when its decoded Creating Access Token
private string CreateAccessToken(ApplicationUser user, string sessionId)
{
var tokenHandler = new JwtSecurityTokenHandler();
var key = Encoding.ASCII.GetBytes(_jwtSecret);

var tokenDescriptor = new SecurityTokenDescriptor
{
Subject = new ClaimsIdentity(new Claim[]
{
new Claim(ClaimTypes.Name, user.UserName.ToString()),
new Claim(JwtRegisteredClaimNames.Jti, sessionId),
new Claim(JwtRegisteredClaimNames.Sub, user.Id),
}),
Expires = DateTime.UtcNow.AddMinutes(30),
SigningCredentials = new(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature),
Issuer = "https://localhost:7059",
Audience = "http://localhost:5173"
};

var token = tokenHandler.CreateToken(tokenDescriptor);
var tokenStr = tokenHandler.WriteToken(token);

return tokenStr;
}
private string CreateAccessToken(ApplicationUser user, string sessionId)
{
var tokenHandler = new JwtSecurityTokenHandler();
var key = Encoding.ASCII.GetBytes(_jwtSecret);

var tokenDescriptor = new SecurityTokenDescriptor
{
Subject = new ClaimsIdentity(new Claim[]
{
new Claim(ClaimTypes.Name, user.UserName.ToString()),
new Claim(JwtRegisteredClaimNames.Jti, sessionId),
new Claim(JwtRegisteredClaimNames.Sub, user.Id),
}),
Expires = DateTime.UtcNow.AddMinutes(30),
SigningCredentials = new(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature),
Issuer = "https://localhost:7059",
Audience = "http://localhost:5173"
};

var token = tokenHandler.CreateToken(tokenDescriptor);
var tokenStr = tokenHandler.WriteToken(token);

return tokenStr;
}
{
"unique_name": "Test",
"jti": "SESS73ab5ea8-2c67-42a9-ba6e-96241dad5b0a",
"sub": "bababf79-a2bc-49b3-9690-206acd714bd0",
"nbf": 1732389177,
"exp": 1732390948,
"iat": 1732389177,
"iss": "https://localhost:7059",
"aud": "http://localhost:5173"
}
{
"unique_name": "Test",
"jti": "SESS73ab5ea8-2c67-42a9-ba6e-96241dad5b0a",
"sub": "bababf79-a2bc-49b3-9690-206acd714bd0",
"nbf": 1732389177,
"exp": 1732390948,
"iat": 1732389177,
"iss": "https://localhost:7059",
"aud": "http://localhost:5173"
}
7 replies
CC#
Created by Cydo on 10/25/2024 in #help
✅ When should you or shouldnt you return nested data in an api call?
Let's say I have a these tables in my database, Project, Members, Task, Comments and I want to return paginated data so a user can view all their projects, then view the tasks and members associated with the project and the comments associated with the individual tasks. Would you do this as all separate queries or do one query that returns all the data nested and paginated? Here's my current thought process.
- Load all Projects paginated - One api call only project data
- Once you have the list of Projects a user can click one

- When a Project page loads another api call will fire and get all the Tasks paginated.

Now this is where I get a little confused
- When you load the Task page the api should return all the Task data, but does it also return the Comments in the same api call? How would they be paginated?

- Or do you do a 2nd api call that returns all the comments paginated. Keeping every api call super specific and only doing one thing.
- Load all Projects paginated - One api call only project data
- Once you have the list of Projects a user can click one

- When a Project page loads another api call will fire and get all the Tasks paginated.

Now this is where I get a little confused
- When you load the Task page the api should return all the Task data, but does it also return the Comments in the same api call? How would they be paginated?

- Or do you do a 2nd api call that returns all the comments paginated. Keeping every api call super specific and only doing one thing.
13 replies
CC#
Created by Cydo on 9/19/2024 in #help
✅ How should updating Database work with Kanban style app work?
No description
7 replies
CC#
Created by Cydo on 9/16/2024 in #help
✅ Having trouble handling errors when registering a new user
So my application is a .NET web api with a React front end, and im trying to handle all my errors on the backend during the register process and I dont seem to understand how errors are handled. When a user signs up im trying to account for all possible issues they could run into. For example wrong password length, missing special characters, uppercase, etc and then the same for email for example is it a valid email, is there any mistakes in the email for instance test@test,com as opposed to [email protected] I noticed that modelstate is always valid with email part, but for password things seem to work. the error that gets thrown when the email is not in a correct format actually comes from here var result = await _userManager.CreateAsync(user, registerRequestDTO.Password); and it throws this error "Username 'test1@test,com' is invalid, can only contain letters or digits." and I can't seem to figure out how to make it handle this error correctly so that it is in the format below, I don't think I can just hard code it because what if a different error is thrown, i want it to always come back in the format, Key: ['array of errors', '...' ] like below.

"errors": {
"ConfirmPassword": [
"Passwords do not match."
]
},

"errors": {
"ConfirmPassword": [
"Passwords do not match."
]
},
But I'm not sure how to achieve that. This is what I set up for my code currently and that specific error always hits the ApplicationException I'll attach the code in a gist since there is so much of it https://gist.github.com/CydoEntis/2d9160d6867651f8209214334779a3fe
6 replies
CC#
Created by Cydo on 9/15/2024 in #help
✅ Unit of Work for just Repositories or Services as well?
When you create a unit of work and you add repositories inside of it so you aren't constantly newing up additional ones across your applications, should services also be added to the same unit of work? Another unit of work or not at all? I don't see the harm in adding services to the unit of work so would that be a valid thing to do or will that cause issues somehow?
1 replies
CC#
Created by Cydo on 9/14/2024 in #help
✅ How to hide sensitive data, like database credentials api keys etc
I know you enter this stuff in your appsettings.json, but from what I can see your apisettings.json gets stored in your gitrepo, so is there something like a .env file in the .NET ecosystem where i can store everything in there omit it from git and then load that data into appsettings.json? Everything I found so far is talking about setting the variables in an azure key vault or setting them inside of the terminal, am I missing something here? I'm just developing locally atm but even if i deploy this app, i dont think I will be using azure because of how expensive it is and would like to not use the key vault because of that, but at the same time i dont want any of this to be on my git repo for random ppl to potentially see as well.
16 replies
CC#
Created by Cydo on 9/6/2024 in #help
✅ What is considered enough knowledge to start applying?
No description
25 replies
CC#
Created by Cydo on 8/27/2024 in #help
✅ Why Red Underline on Views?
No description
15 replies
CC#
Created by Cydo on 8/18/2024 in #help
✅ MVC application routing issue.
Why does the following code not work
<div>
<a asp-route-productId="@product.Id"
class="btn btn-primary bg-gradient border-0 form-control">
Details
</a>
</div>
<div>
<a asp-route-productId="@product.Id"
class="btn btn-primary bg-gradient border-0 form-control">
Details
</a>
</div>
But this code does
<div>
<a href="@Url.Action("Details", "Home", new { productId = product.Id })"
class="btn btn-primary bg-gradient border-0 form-control">
Details
</a>
</div>
<div>
<a href="@Url.Action("Details", "Home", new { productId = product.Id })"
class="btn btn-primary bg-gradient border-0 form-control">
Details
</a>
</div>
54 replies
CC#
Created by Cydo on 8/10/2024 in #help
How to create migrations in rider, when you have multiple projects in one Solution?
No description
37 replies
CC#
Created by Cydo on 8/10/2024 in #help
✅ Rider Not Creating a New Project in an existing solution correctly?
No description
21 replies