Showing posts with label ASP.NET Core. Show all posts
Showing posts with label ASP.NET Core. Show all posts

Monday, March 18, 2024

Regular Expressions 101

 /^.*ezsend\/link\/([\w-]+)\/([\w-]+)\/([\w-]+)?.*$

Regular expression sounds very intimating....Good site for testing - https://regex101.com/

Let's break down the regular expression ^.*ezsend\/link\/([\w-]+)\/([\w-]+)\/([\w-]+)?.*$:

^: Asserts the start of the string.

.*: Matches any character (.) zero or more times (*). This part is used to match any characters before the "ezsend/link" part of the URL.

ezsend\/link\/: Matches the literal string "ezsend/link/".

([\w-]+): This is the first capturing group, denoted by the parentheses. It matches one or more word characters (\w) or hyphens (-). Word characters include letters, digits, and underscores.

\/: Matches the forward slash / after the first token.

([\w-]+): This is the second capturing group, which matches the second token similar to the first one.

\/: Matches the forward slash / after the second token.

([\w-]+)?: This is the third capturing group, matching the third token similar to the first two. The ? makes this group optional, allowing the pattern to match URLs with or without a third token.

.*: Matches any remaining characters after the third token, if present.

$: Asserts the end of the string.

Here's how this regex works:

It starts by matching any characters before "ezsend/link/", ensuring that the URL contains this path.

Then, it matches the three tokens separated by forward slashes, allowing hyphens in each token.

The third token is optional, indicated by the ?.

Finally, it matches any remaining characters in the URL until the end of the string.

This regular expression is designed to capture the three tokens from URLs containing hyphens in each token, such as "xSwAAAAAAAA", "l-f1bdGQ92Q", and "ftG-CggAAAA".


ModelState.Clear vs TryValidateModel(manageModel)

 


`ModelState.Clear()` and `TryValidateModel(manageModel)` are both methods commonly used in ASP.NET Core applications to manage model validation.

1. `ModelState.Clear()`: This method is used to clear any model state errors that have been added to the `ModelState` dictionary. In ASP.NET Core, `ModelState` is a dictionary-like object that holds information about the state of model binding and validation for a given request. It stores information about model properties and their validation errors. Calling `ModelState.Clear()` removes all existing validation errors, effectively resetting the model state to a clean state.

2. `TryValidateModel(manageModel)`: This method is used to manually trigger model validation for a specified model object (`manageModel` in this case). By calling `TryValidateModel(manageModel)`, you are instructing ASP.NET Core to validate the specified model object against any validation rules defined in its associated data annotations or custom validation logic. If validation succeeds, the method returns `true`, indicating that the model is valid. If validation fails, the method returns `false`, and any validation errors are added to the `ModelState` dictionary.

In summary, `ModelState.Clear()` is used to clear any existing model state errors, while `TryValidateModel(manageModel)` is used to manually trigger model validation for a specific model object. These methods are often used together to ensure that model validation is performed correctly in ASP.NET Core applications.

Tuesday, November 28, 2023

MVC - C# Reference

? A question mark (?) is used to denote a nullable type. A nullable type can represent all the values of its underlying non-nullable value type plus an additional null value. This is particularly useful when dealing with value types, which cannot normally be assigned a null value. 

int? nullableInt = 42;  // Valid assignment

nullableInt = null;    // Valid assignment

DateTime? nullableDateTime = DateTime.Now;  // Valid assignment

nullableDateTime = null;                    // Valid assignment

?? The ?? operator, also known as the null-coalescing operator, is used for handling null values in a concise way. It provides a way to return a default value when the left-hand operand is null. 

string proName = pro?[0]?.Name ?? "Default Value";

The #pragma warning disable directive is used in C# to disable specific compiler warnings: #pragma warning disable CS8602

Target-typed new expressions are a feature introduced in C# 9.0. They allow you to omit the type in the new expression when the type can be inferred from the surrounding context. This helps reduce redundancy in your code and makes it more concise.        

// Without target-typed new expression

List<string> names = new List<string>();

// With target-typed new expression (C# 9.0 and later)

List<string> names = new(); 

The update-database command is part of the Entity Framework Migrations workflow and is crucial for keeping the database schema in sync with your code changes. It's a powerful tool that automates the process of evolving the database as your application evolves, making it easier to manage changes to your data model over time.

        Applying Database Migrations/Creating the Initial Database/Applying Subsequent     Migrations/Rolling Back Migrations

In Entity Framework, the DbContext class provides a set of methods to interact with the underlying database using LINQ queries. Here are some common methods associated with DbSet that allow you to perform various operations:

  1. Query Operations:

    • FirstOrDefault, SingleOrDefault: Retrieve the first or a single entity that satisfies a condition.
    • Where: Filter entities based on a condition.
    • OrderBy, OrderByDescending, ThenBy, ThenByDescending: Order entities based on one or more properties.
  2. Insert/Update Operations:

    • Add, AddRange: Add a new entity or a collection of entities to the context.
    • Attach: Attaches an entity or a disconnected graph of entities to the context.
    • Update: Marks an entity or entities as modified.
    • Remove, RemoveRange: Remove an entity or a collection of entities from the context.
  3. Save Changes:

    • SaveChanges, SaveChangesAsync: Persist changes made in the context to the underlying database.
  4. Bulk Operations:

    • Entity Framework Core (EF Core) has introduced extensions like BulkInsert, BulkUpdate, BulkDelete for handling bulk operations efficiently.
  5. Raw SQL Queries:

    • FromSqlRaw, FromSqlInterpolated: Execute raw SQL queries.