Sunday, June 9, 2024
Serilog Sinks
Sunday, May 26, 2024
Use case of .http files in Visual Studio
An API .http file a feature provided by Visual Studio 2022 for testing ASP.NET Core projects. This serves as a convenient way to send HTTP requests and view responses directly within the Visual Studio IDE.
Further more, the VS 2022 .http file editor allows you to define HTTP requests that you want to send to test API endpoints.
It comes with an easy way to specify the details of HTTP requests including
- URL
- HTTP method (GET, POST, PUT.,DELETE)
- headers,
- query parameters
- request body
After completion of the a request, Visual Studio IDE displays the response directly within the editor itself, allowing an easy way to view the response status code, headers, and body.
To learn mor visit this link.
Cheers,
Samitha
Saturday, April 20, 2024
ASP.NET Web Applications with Content Security Policy
Content Security Policy (CSP) can be used to protect any ASP.NET web application by identifying
- Cross-site Scripting (XSS) attacks
- SQL or data injection attacks.
Cheers,
Samitha
Monday, April 8, 2024
.Net core 6 db migration error
I got following error when trying to run the migrations on a .NET Core 6 Web API.
Unable to create an object of type 'MyDBContext'
After tarting out different approaches following changes made migrations work.
1. MyDbContext
public class MyDbContext : DbContext, IMyDbContext
{
public MyDbContext()
{
}
public MyDbContext(DbContextOptions<MyDbContext> options) : base(options)
{
protected override void OnConfiguring(DbContextOptionsBuilder
optionsBuilder)
{
optionsBuilder.UseSqlServer("Data Source=localhost;Initial Catalog = Library;Integrated Security=True;Trusted_Connection=True;TrustServerCertificate=True");
}
//...more code
}
2. Program.cs
builder.Services.ConfigureLoggerService();
//more options
options.UseSqlServer(builder.Configuration.GetConnectionString
("DefaultConnection")));
Cheers,
Samitha
Friday, March 29, 2024
Append JavaScript to html body
Following JavaScript code will append an external JavaScript file to the document body. This can be used as guide to load a JavaScript file on the fly.
<script type="text/javascript">
var newScript = document.createElement('script');
newScript.type = 'text/javascript';
newScript.async = true;
newScript.src = 'https://somedomain.com/JsFileName.js';
document.getElementsByTagName("body")[0].appendChild(newScript);
</script>
cheers,
Samitha
Sunday, March 3, 2024
Auto Generate .gitignore file
This is a configuration file that tells Git what to ignore when committing changes to Git based repositories (the ones in GitHub).
The .gitignore file controls what files to ignore when committing changes to Git repositories. The good new is is there is a tool called gitignore.io. This tool will generate such files for us.
I like this as it allows to type programming languages/platforms and generates the file for us
Cheers,
Samitha
Friday, January 26, 2024
C# download file from a given URL
If you want to download a file in C # form a given url following code snippet can be used.
public static async Task Download(string remoteUri, string directory)
{
try
{
string filePath = Path.Combine(directory, Path.GetFileName(remoteUri));
var httpClient = new HttpClient();
if (!Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
}
using var stream = await httpClient.GetStreamAsync(remoteUri);
using var fileStream = new FileStream(filePath, FileMode.Create);
await stream.CopyToAsync(fileStream);
}
catch (Exception)
{
throw;
}
}
When calling the above method we can user as
Download("https://abc.com/Content/Images/imagename.jpg", Directory.GetCurrentDirectory() + "\\images\\").Wait();
Note that this code is compatible with .Net 6 and .Net 7.
cheers,
Samitha