Meet The Author

I'm Ethan Jackson, An 25 years old blogger Currently living in London, United Kingdom. I'm a Skilled Blogger, Part Time web Developer And Creating new things as a web Designer.

author

Using Request.Form[UniqueID] to Safely Retrieve Encrypted Form Values in ASP.NET

Leave a Comment

Form controls, such as hidden fields, textboxes, or HTML input elements, are frequently used to hold temporary or encrypted values when creating secure ASP.NET applications. During postback, these frequently store information such as encrypted PAN numbers, OTP tokens, or encrypted session keys.

A typical pattern looks like this:
string encryptedText = Request.Form[hdnPANEnc.UniqueID];
// or
string encryptedText = Request.Form[panidvalue.UniqueID];

But many developers wonder:

“Is this safe? Will it throw an error? Are there any disadvantages?”

Let’s break this down step by step and understand how it works internally — and how to use it safely.
Understanding How Request.Form[UniqueID] Works

ASP.NET Web Forms uses a naming container hierarchy, meaning every control is given a unique, fully qualified name in the rendered HTML.

When a form is posted, ASP.NET automatically maps each posted field back to the server control using that control’s UniqueID.

Example Controls
<input type="hidden" name="ctl00$MainContent$hdnPANEnc" value="ENCRYPTED_DATA" />

<asp:HiddenField ID="hdnPANEnc" runat="server" />

<asp:TextBox ID="Panidvalue" runat="server" placeholder="Enter PAN"></asp:TextBox>

<input type="text" id="Panid" value="" placeholder="Enter PAN" autocomplete="off" />


Server-side Retrieval

string encryptedText = Request.Form[hdnPANEnc.UniqueID];
// or
string encryptedText = Request.Form[Panidvalue.UniqueID];


This correctly retrieves the posted value — whether it comes from a hidden field or a textbox — after a postback.

Why It’s Safe and Reliable

It does not throw exceptions if the field exists in the posted form.
It’s the recommended method for accessing posted values outside data-binding or control events.

ASP.NET automatically URL-decodes form data, so encrypted strings containing +, /, or = are handled safely.

You can safely use it to retrieve encrypted or masked values (like PAN, OTP, or token data).
Possible Disadvantages and Caveats

Although this approach works perfectly, you should be aware of a few potential caveats:
 

1. Hidden Fields Are Visible in Browser Developer Tools
Even encrypted data stored in a hidden field can be seen and modified using browser “Inspect Element.”

Best Practice
Always decrypt and validate the value on the server side before using it.
string encryptedText = Request.Form[hdnPANEnc.UniqueID];
if (!string.IsNullOrEmpty(encryptedText))
{
    encryptedText = HttpUtility.UrlDecode(encryptedText);
    string decryptedPAN = Decrypt(encryptedText);
    // Re-validate with DB or checksum if required
}


2. ID Changes in Nested Controls
When a control exists inside a Master Page, Repeater, or UpdatePanel, its HTML name changes dynamically.

Using .UniqueID ensures ASP.NET can still map the posted form value correctly.

Using .ID may fail because it only represents the local control name, not the full hierarchy.

You’re already doing it correctly with .UniqueID.

3. AJAX or Custom POST Requests
If your form is submitted using JavaScript (e.g., fetch() or $.ajax()), hidden or textbox values might not post unless you serialize the entire form properly.

Example
var formData = new FormData(document.getElementById("form1"));
fetch('YourPage.aspx', {
    method: 'POST',
    body: formData
});


This ensures all form controls, including hidden fields and textboxes, are sent to the server.

4. Encoding Issues (Rare)

Some encryption outputs (like Base64) contain characters such as +, /, or =.
Browsers automatically URL-encode them during submission.

To avoid decoding mismatches:
string encryptedText = HttpUtility.UrlDecode(Request.Form[hdnPANEnc.UniqueID]);

Security Best Practices
RecommendationDescription
Encrypt sensitive dataBefore assigning values to hidden fields or textboxes
Decrypt & validate server-sideNever trust client-side values directly
Avoid plain text storageNever expose PAN, UPI ID, or passwords
Use ViewState encryption or SessionFor higher security and tamper-proof data

Final Verdict
CheckpointResult
Works without errorsYes
Safe to useYes (with encryption)
Handles nested controlsYes
Security caveatsHidden fields are user-editable
RecommendedYes, with server-side validation

Example Secure Implementation

ASPX

<asp:HiddenField ID="hdnPANEnc" runat="server" />
<asp:TextBox ID="Panidvalue" runat="server" placeholder="Enter PAN"></asp:TextBox>
C# (Code-Behind)
// Encrypt and assign value
hdnPANEnc.Value = Encrypt(userPAN);

// Retrieve securely during postback
string encryptedText = Request.Form[hdnPANEnc.UniqueID];
if (!string.IsNullOrEmpty(encryptedText))
{
    string decryptedPAN = Decrypt(HttpUtility.UrlDecode(encryptedText));
    // Use decryptedPAN safely after validation
}

// Also handle textbox input
string inputPAN = Request.Form[Panidvalue.UniqueID];
if (!string.IsNullOrEmpty(inputPAN))
{
    // Optionally encrypt, sanitize, or validate here
}
Conclusion

Using Request.Form[control.UniqueID] — whether for a hidden field, textbox, or custom <input> — is a reliable, efficient, and flexible way to retrieve posted values in ASP.NET Web Forms.

Just remember

  • It won’t throw errors if the field exists.

  • It automatically handles URL-decoding.

  • The only risk lies in trusting client-side data — always decrypt and verify it on the server.

By following these best practices, you ensure both security and stability in your ASP.NET form-handling logic.

Windows Hosting Recommendation

HostForLIFE.eu receives Spotlight standing advantage award for providing recommended, cheap and fast ecommerce Hosting including the latest Magento. From the leading technology company, Microsoft. All the servers are equipped with the newest Windows Server 2022 R2, SQL Server 2022, ASP.NET Core 10.0 , ASP.NET MVC, Silverlight 5, WebMatrix and Visual Studio Lightswitch. Security and performance are at the core of their Magento hosting operations to confirm every website and/or application hosted on their servers is highly secured and performs at optimum level. mutually of the European ASP.NET hosting suppliers, HostForLIFE guarantees 99.9% uptime and fast loading speed. From €3.49/month , HostForLIFE provides you with unlimited disk space, unlimited domains, unlimited bandwidth,etc, for your website hosting needs.
 
https://hostforlifeasp.net/
Read More

Background Tasks Using Hosted Services in ASP.NET Core Without Hangfire

Leave a Comment

Sending emails, clearing away old logs, processing files, and updating reports are just a few examples of the background tasks that are frequently required in real-world systems without interfering with user requests.

Many developers immediately turn to frameworks such as Azure Functions, Quartz.NET, or Hangfire.
However, what if you're looking for something integrated directly into ASP.NET Core, lightweight, and dependency-free?

The answer is:
Hosted Services — a powerful built-in feature in ASP.NET Core to run background tasks efficiently.

This article will show you how to build background jobs without Hangfire, step-by-step.

1. What Are Background Jobs?

A background job is any process that runs behind the scenes — not directly triggered by a user and not part of the main request/response flow.

Common Examples
  • Sending order confirmation emails after checkout

  • Generating and emailing PDF reports

  • Cleaning up temporary files

  • Syncing data with third-party APIs at intervals

These tasks can take time, and you don't want them to slow down user requests.
That's where Hosted Services come in.

2. What Are Hosted Services in ASP.NET Core?

ASP.NET Core includes a background task framework out of the box using IHostedService.

You can think of it as a service that starts with your application and runs continuously in the background.

There are mainly two types:

  1. IHostedService – Run logic at startup and shutdown.

  2. BackgroundService – A helper base class for continuous or scheduled jobs.

3. Architecture Overview

Below is a high-level diagram of how Hosted Services work in ASP.NET Core.

Flowchart: Background Job Architecture
+----------------------------------+
| ASP.NET Core Application         |
| (Web API / MVC / Razor / Blazor) |
+-------------------+--------------+
                    |
                    v
+----------------------------------+
| Hosted Service (Background Task) |
| - Runs Independently             |
| - Starts with Application        |
| - Handles Long/Recurring Jobs    |
+-------------------+--------------+
                    |
                    v
+----------------------------------+
| Services / Database / APIs       |
| (Email, File System, Cleanup)    |
+----------------------------------+
4. Step-by-Step Implementation

Let's create a simple background job that runs every 5 minutes and cleans up old logs from the database.

Step 1: Create the Hosted Service Class
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

public class LogCleanupService : BackgroundService
{
    private readonly ILogger<LogCleanupService> _logger;
    private readonly IServiceProvider _serviceProvider;

    public LogCleanupService(ILogger<LogCleanupService> logger, IServiceProvider serviceProvider)
    {
        _logger = logger;
        _serviceProvider = serviceProvider;
    }

    protected override async Task ExecuteAsync(CancellationToken stoppingToken)
    {
        _logger.LogInformation("Log Cleanup Service started.");

        while (!stoppingToken.IsCancellationRequested)
        {
            await DoCleanupAsync();
            await Task.Delay(TimeSpan.FromMinutes(5), stoppingToken); // Runs every 5 minutes
        }

        _logger.LogInformation("Log Cleanup Service stopped.");
    }

    private async Task DoCleanupAsync()
    {
        try
        {
            using (var scope = _serviceProvider.CreateScope())
            {
                var dbContext = scope.ServiceProvider.GetRequiredService<MyAppDbContext>();

                var oldLogs = dbContext.Logs
                    .Where(l => l.CreatedDate < DateTime.UtcNow.AddDays(-30))
                    .ToList();

                dbContext.Logs.RemoveRange(oldLogs);
                await dbContext.SaveChangesAsync();

                _logger.LogInformation($"{oldLogs.Count} old logs cleaned up successfully.");
            }
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "Error while cleaning up logs.");
        }
    }
}
Step 2: Register the Service in Program.cs
var builder = WebApplication.CreateBuilder(args);

// Register your Hosted Service
builder.Services.AddHostedService<LogCleanupService>();

// Register DbContext and other dependencies
builder.Services.AddDbContext<MyAppDbContext>(options =>
    options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));

var app = builder.Build();

app.MapControllers();
app.Run();

That's it!
Now your background service will automatically start when the application starts.

5. Handling Dependency Injection

When working inside a background service, never directly inject scoped services (like DbContext).
Instead, use IServiceProvider.CreateScope() — as shown above — to create a new service scope.

This ensures clean resource management and prevents memory leaks.

6. Adding Multiple Background Services

You can register multiple hosted services — each handling a different task:

builder.Services.AddHostedService<EmailSenderService>();
builder.Services.AddHostedService<DataSyncService>();
builder.Services.AddHostedService<ReportGeneratorService>();

Each one will run independently in its own background thread.

7. Graceful Shutdown and Cancellation

ASP.NET Core automatically handles graceful shutdowns for hosted services.
When the app stops (or is restarted), it sends a cancellation token (stoppingToken) to stop the job safely.

To handle it cleanly:

if (stoppingToken.IsCancellationRequested)
{
    _logger.LogInformation("Service stopping...");
    break;
}

8. Advanced Scenarios

Scheduled Jobs

You can make jobs run at specific times using Cronos (a lightweight Cron parser):

using Cronos;

private readonly CronExpression _cron = CronExpression.Parse("0 * * * *"); // every hour

Then calculate the next occurrence using:

var next = _cron.GetNextOccurrence(DateTime.UtcNow);

Queue-Based Background Processing

For more complex use cases (like queued background tasks), use BackgroundTaskQueue with Channel or ConcurrentQueue.

9. Benefits of Using Hosted Services

FeatureBenefit
No extra dependencyBuilt directly into .NET Core
LightweightIdeal for microservices and small apps
ReliableStarts/stops with the app lifecycle
FlexibleRun periodic, continuous, or one-time tasks
SecureFull DI and configuration support

10. When Not to Use Hosted Services

  • For long-running or heavy workloads, use Azure Functions, Worker Services, or Hangfire.

  • For distributed job coordination, prefer message queues (RabbitMQ, Kafka).

  • Hosted Services are best for simple, internal background tasks within a single app instance.

Conclusion

You don't always need heavy frameworks like Hangfire for background processing.
ASP.NET Core's Hosted Services provide a clean, native way to manage background jobs — with full integration into dependency injection, logging, and configuration.

They're:

  • Simple to build

  • Reliable to run

  • Perfect for small to medium workloads

By mastering Hosted Services, you gain a powerful tool to run scheduled, continuous, or on-demand background tasks all without adding any external library.

Windows Hosting Recommendation

HostForLIFEASP.NET receives Spotlight standing advantage award for providing recommended, cheap and fast ecommerce Hosting including the latest Magento. From the leading technology company, Microsoft. All the servers are equipped with the newest Windows Server 2022 R2, SQL Server 2022, ASP.NET Core 10.0, ASP.NET MVC, Silverlight 5, WebMatrix and Visual Studio Lightswitch. Security and performance are at the core of their Magento hosting operations to confirm every website and/or application hosted on their servers is highly secured and performs at optimum level. mutually of the European ASP.NET hosting suppliers, HostForLIFE guarantees 99.9% uptime and fast loading speed. From €3.49/month , HostForLIFE provides you with unlimited disk space, unlimited domains, unlimited bandwidth,etc, for your website hosting needs.
 
https://hostforlifeasp.net/

 

Read More

How to Create Custom Middleware in ASP.NET Core?

Leave a Comment

Middleware: What is it?
A middleware is a little piece of software that handles HTTP requests and responses in ASP.NET Core.
Every middleware operates in a pipeline and is capable of:

  • Execute a task prior to the subsequent middleware execution.
  • After it runs, take a certain action, or
  • even prevent the request from being processed further.

To put it simply, middleware can examine, alter, or end a request or response, much like a checkpoint in the request pipeline.

Default Middleware Examples

ASP.NET Core comes with built-in middleware such as:

  • UseRouting() — Handles URL routing

  • UseAuthentication() — Handles login/auth

  • UseAuthorization() — Manages access control

  • UseStaticFiles() — Serves static files like images or CSS

You can also create your own custom middleware to add specific logic (like logging, validation, or tracking).

Step-by-Step: Creating Custom Middleware

Let’s create a simple custom middleware that logs each incoming request and outgoing response.

Step 1. Create the Middleware Class
using Microsoft.AspNetCore.Http;
using System.Threading.Tasks;
using System;

public class MyCustomMiddleware
{
    private readonly RequestDelegate _next;

    // Constructor to accept the next middleware in the pipeline
    public MyCustomMiddleware(RequestDelegate next)
    {
        _next = next;
    }

    // Main method to handle request and response
    public async Task InvokeAsync(HttpContext context)
    {
        // Code before the next middleware runs
        Console.WriteLine($" Request received: {context.Request.Method} {context.Request.Path}");

        // Call the next middleware in the pipeline
        await _next(context);

        // Code after the next middleware runs
        Console.WriteLine($" Response sent: {context.Response.StatusCode}");
    }
}
Step 2. Create an Extension Method (for cleaner code)

It’s good practice to add an extension method so you can easily use your middleware.

using Microsoft.AspNetCore.Builder;

public static class MyCustomMiddlewareExtensions
{
    public static IApplicationBuilder UseMyCustomMiddleware(this IApplicationBuilder builder)
    {
        return builder.UseMiddleware<MyCustomMiddleware>();
    }
}
Step 3. Register the Middleware in Program.cs

In .NET 6+, middleware is added in the request pipeline using the app object.

var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

// Custom middleware registration
app.UseMyCustomMiddleware();

// Example controller routing
app.MapControllers();

app.Run();

Step 4. Run and Test

Now run your application (Ctrl + F5).

When you send a request (like GET /api/values), you’ll see logs in the console:

Request received: GET /api/values
Response sent: 200

Your custom middleware successfully intercepted and logged the request and response!

How Middleware Works Internally?

When a request comes in:

  1. The first middleware receives it.

  2. It can do something (like logging) and call the next middleware.

  3. The request flows through all middleware components.

  4. The response flows back through them in reverse order.

Visual Flow Diagram
Request → [Middleware 1] → [Middleware 2] → [Middleware 3] → Endpoint
             ↑                                      ↓
          Response ← [Middleware 3] ← [Middleware 2] ← [Middleware 1]
When to Use Custom Middleware

Custom middleware is useful for:

  • Logging requests and responses

  • Handling exceptions globally

  • Adding custom headers or tokens

  • Measuring performance

  • Implementing request filters (e.g., IP blocking)

Example: Adding a Header Middleware

Here’s another short example that adds a custom header to all responses:

public class HeaderMiddleware
{
    private readonly RequestDelegate _next;

    public HeaderMiddleware(RequestDelegate next)
    {
        _next = next;
    }

    public async Task InvokeAsync(HttpContext context)
    {
        await _next(context);
        context.Response.Headers.Add("X-Powered-By", "SandhiyaCustomMiddleware");
    }
}

Register it in Program.cs:

app.UseMiddleware<HeaderMiddleware>();

Every response from your app will now include:

X-Powered-By: SandhiyaCustomMiddleware

Summary

StepAction
1Create a middleware class with InvokeAsync(HttpContext)
2Add an extension method for better reusability
3Register it in the request pipeline using the app.UseMyCustomMiddleware()
4Test by sending a request and checking the console or response headers

Conclusion

Custom middleware gives you complete control over the request and response pipeline in ASP.NET Core.

It helps you write clean, reusable, and centralized logic for logging, authentication, or any pre/post-processing.

In short: Middleware is the heart of ASP.NET Core request handling — and custom middleware lets you extend that heart for your unique business needs.

Windows Hosting Recommendation

HostForLIFE.eu receives Spotlight standing advantage award for providing recommended, cheap and fast ecommerce Hosting including the latest Magento. From the leading technology company, Microsoft. All the servers are equipped with the newest Windows Server 2022 R2, SQL Server 2022, ASP.NET Core 10.0 , ASP.NET MVC, Silverlight 5, WebMatrix and Visual Studio Lightswitch. Security and performance are at the core of their Magento hosting operations to confirm every website and/or application hosted on their servers is highly secured and performs at optimum level. mutually of the European ASP.NET hosting suppliers, HostForLIFE guarantees 99.9% uptime and fast loading speed. From €3.49/month , HostForLIFE provides you with unlimited disk space, unlimited domains, unlimited bandwidth,etc, for your website hosting needs.
 
https://hostforlifeasp.net/
Read More

Jenkins CI/CD for ASP.NET Core + Angular App Deployment

Leave a Comment

Applications built using Angular for the frontend and ASP.NET Core for the backend are frequently deployed by hand, requiring file copies, Visual Studio publishing, IIS restarts, and other steps. Manual deployment becomes dangerous as your team increases, although it works well for small projects. One minor error could overwrite settings or result in downtime.

Jenkins' Continuous Integration and Continuous Deployment (CI/CD) technology can help with it.
Every time you push code, it swiftly and safely builds, tests, and deploys your project. I'll walk you through the process of utilizing the Jenkins pipeline to deploy a full-stack application (Angular + ASP.NET Core) on a Windows server (IIS) in this post.

Requirements

Before we start, make sure you have:

  • Installed Jenkins (Windows or Linux)

  • Installed .NET SDK (e.g. .NET 8.0) on the Jenkins server

  • Installed Node.js and Angular CLI

  • Installed Git (for source code checkout)

  • Access to your IIS server for deployment

Project Structure

Below is a common folder structure we’ll work with:

HFLDemoApp/
 ├── WebAPI/             # ASP.NET Core Backend
 ├── ClientApp/          # Angular Frontend
 ├── Jenkinsfile         # Jenkins pipeline script
 ├── deploy.ps1          # PowerShell script for IIS deployment

Step 1. Create a Jenkins Pipeline Job

  1. Open Jenkins Dashboard → New Item → Pipeline

  2. Give it a name, e.g., RajeshDemoApp-CI-CD

  3. Under Pipeline definition, select Pipeline script from SCM

  4. Choose Git, and provide your repo URL.

  5. Jenkins will now read the Jenkinsfile from your repository.

Step 2. Jenkinsfile Configuration

Here’s a simple Jenkinsfile example for our project:

pipeline {
    agent any

    stages {
        stage('Checkout Code') {
            steps {
                git branch: 'main', url: 'https://github.com/your-repo'
            }
        }

        stage('Build Angular') {
            steps {
                dir('ClientApp') {
                    bat 'npm install'
                    bat 'ng build --configuration production'
                }
            }
        }

        stage('Build .NET Core') {
            steps {
                dir('WebAPI') {
                    bat 'dotnet restore'
                    bat 'dotnet publish -c Release -r win-x64 -o ../publish'
                }
            }
        }

        stage('Deploy to IIS') {
            steps {
                bat 'powershell .\\deploy.ps1'
            }
        }
    }
}

Note:
If you’re using Linux agents, replace bat with sh.

Step 3. Create PowerShell Script for Deployment

Let’s create a PowerShell file named deploy.ps1 at the root of your repo.

This script will:

  • Stop IIS site

  • Backup old files

  • Copy new build files

  • Restart IIS site

Write-Host "Starting deployment..."

$source = "C:\Jenkins\workspace\MyApp-CI-CD\publish"
$destination = "C:\inetpub\wwwroot\RajeshDemoApp"
$backup = "C:\Backup\MyApp_" + (Get-Date -Format "yyyyMMdd_HHmmss")

# Stop IIS
iisreset /stop

# Backup existing site
if (Test-Path $destination) {
    Copy-Item $destination $backup -Recurse
    Write-Host "Backup created at $backup"
}

# Copy new files
Copy-Item $source\* $destination -Recurse -Force

# Start IIS
iisreset /start

Write-Host "Deployment completed successfully."

Step 4. Handle Configuration Files

We usually have environment-specific files like:

  • appsettings.Production.json

  • environment.prod.ts

You can use a small PowerShell snippet to replace these files based on environment before publishing.

Example:

Copy-Item ".\Configs\Production\appsettings.json" ".\WebAPI\appsettings.json" -Force

Step 5. Run the Pipeline

  1. Commit and push your code (including Jenkinsfile).

  2. Go to Jenkins → click Build Now.

  3. Jenkins will run each stage — build Angular, build .NET, deploy to IIS.

Step 6. Troubleshooting Tips

If deployment fails:

  • Check Jenkins console logs (very helpful).

  • Make sure IIS user has write permission to your deployment folder.

  • Ensure Node.js, Angular CLI, and .NET SDK paths are configured in Jenkins.

Step 7. Benefits of Using CI/CD

After setting this up once, your deployments become:

  • Automatic – no manual file copying

  • Consistent – same steps every time

  • Fast – 1-click deployment from Jenkins

  • Safe – automatic backup and rollback

Conclusion

By combining Jenkins, ASP.NET Core, and Angular, we can automate our entire deployment process.

No more “it works on my machine” issues — your build, test, and deployment happen exactly the same way every time.

Windows Hosting Recommendation

HostForLIFE receives Spotlight standing advantage award for providing recommended, cheap and fast ecommerce Hosting including the latest Magento. From the leading technology company, Microsoft. All the servers are equipped with the newest Windows Server 2022 R2, SQL Server 2022, ASP.NET Core 10.0 , ASP.NET MVC, Silverlight 5, WebMatrix and Visual Studio Lightswitch. Security and performance are at the core of their Magento hosting operations to confirm every website and/or application hosted on their servers is highly secured and performs at optimum level. mutually of the European ASP.NET hosting suppliers, HostForLIFE guarantees 99.9% uptime and fast loading speed. From €3.49/month , HostForLIFE provides you with unlimited disk space, unlimited domains, unlimited bandwidth,etc, for your website hosting needs.
 
https://hostforlifeasp.net/
Read More

Managing ASP.NET Session Problems in Several Tabs of the Browser: Making Sure Popups Have Correct User Data

Leave a Comment

It can be challenging to manage user sessions across several browser tabs in contemporary web applications, particularly when you wish to display dynamic user data via pop-ups like an application form. This blog post will examine a typical ASP.NET session problem and offer a thorough, step-by-step fix.

The Scenario
Imagine this situation:

  • Tab 1: User A logs in.
  • Tab 2: User B logs in in the same browser.
  • You return to Tab 1 and try to apply IPO.

Problem
The pop-up shows User A’s name (from page load).
Backend, however, processes User B’s session data (because the session is shared across tabs).

This mismatch can confuse users and cause data integrity issues.

Why This Happens

ASP.NET sessions are per browser, not per tab.
Tab 1: logs in as User A → Session["ClientFname"] = "A".
Tab 2: logs in as User B → Session["ClientFname"] = "B".
Both tabs now share the same session cookie.

Result: Frontend shows old cached data (User A), while the backend processes the latest session (User B).

Goal

We want to ensure that:

  • The frontend popup always shows the correct client details.
  • The backend request corresponds to the same user shown in the pop-up.
  • Solution: Fetch Data Dynamically from Backend

The most reliable approach is to fetch user session data dynamically from the server when the pop-up is triggered. This ensures the displayed data always matches the backend session.

Step 1. Add a WebMethod in ASP.NET

In your ASPX code-behind, create a WebMethod to return session data:

[System.Web.Services.WebMethod]
public static object GetClientSessionData()
{
    try
    {
        string name = HttpContext.Current.Session["ClientFname"]?.ToString() ?? "";
        string pan = HttpContext.Current.Session["Pan"]?.ToString() ?? "";
        string dp = HttpContext.Current.Session["Depository"]?.ToString() ?? "";

        return new
        {
            ClientFname = name,
            Pan = pan,
            Depository = dp
        };
    }
    catch (Exception ex)
    {
        return new { Error = ex.Message };
    }
}

Tip: You can also create a strong-typed class for clarity instead of returning an anonymous object.



Step 2. Call the WebMethod from JavaScript

When the user clicks Apply IPO, call the WebMethod to get the current session data:

When the user clicks Apply IPO, call the WebMethod to get the current session data:

function submitalldata() {
    // Get UPI ID entered by user
    var UPIID = document.getElementById("<%=txtupiid.ClientID%>").value;

    // Call backend WebMethod to get session data
    $.ajax({
        type: "POST",
        url: "ApplyIPO.aspx/GetClientSessionData",
        data: '{}',  // no parameters needed
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (response) {
            var data = response.d;

            if (data.Error) {
                alert("Error: " + data.Error);
                return;
            }

            // Populate popup fields with session data
            document.getElementById("clname").innerText = data.ClientFname;
            document.getElementById("clpan").innerText = data.Pan;
            document.getElementById("cldpid").innerText = data.Depository;
            document.getElementById("clupi").innerText = UPIID;
            document.getElementById("clupiname").innerText = data.ClientFname; // optional if needed

            // Show popup modal dynamically
            $('#conformModalpopup').modal('show');
        },
        error: function (xhr, status, error) {
            console.error("Error fetching session data:", error);
        }
    });

    // Prevent full postback
    return false;
}
Step 3. What Happens Now

The popup always fetches current session data from the backend.

Even with multiple tabs open, the correct user details are shown.

You avoid mismatches between frontend and backend.

ASPX Markup (Frontend)

<!-- Apply Button -->

<div class="modal-footer">

<a id="applybtn" runat="server" onclick="return submitalldata();">APPLY</a>

</div>

<!-- Popup Modal -->

<div class="modal fade" id="conformModalpopup" role="dialog" aria-labelledby="exampleModalCenterTitle">

<div class="modal-dialog modal-dialog-centered" role="document">

<div class="modal-content newboarderradius modalfooterclass">

<div class="modal-header popupheader">

<h5 class="modal-title"><b>Client Details Verification</b></h5>

<button type="button" class="close clientpopupclose btn-close" data-bs-dismiss="modal" aria-label="Close">

<span>&times;</span>

</button>

</div>

<div class="modal-body">

<div class="verification">

<table class="usertblvalidation">

<tr>

<td class="useralignment">Client Name</td>

<td>:</td>

<td id="clname" runat="server" class="useralignment"></td>

</tr>

<tr>

<td class="useralignment">Pan No</td>

<td>:</td>

<td id="clpan" runat="server" class="useralignment"></td>

</tr>

<tr>

<td class="useralignment">DP ID</td>

<td>:</td>

<td id="cldpid" runat="server" class="useralignment"></td>

</tr>

<tr>

<td class="useralignment">UPI ID</td>

<td>:</td>

<td id="clupi" class="useralignment"></td>

</tr>

<tr>

<td class="useralignment">UPI ID Name</td>

<td>:</td>

<td id="clupiname" class="useralignment"></td>

</tr>

</table>

</div>

</div>

<div class="modal-footer verifiedfooter">

<div class="footerbtnopenclose">

<button type="button" class="btn btn-white verificationpopup" data-bs-dismiss="modal" id="cancelbtn">Cancel</button>

<button type="button" class="btn btn-white modify-popup verificationpopup" id="confirm_txt" onclick="message();">Confirm</button>

</div>

<div class="input-container upidivcontent">

<label id="conform_txtbx"></label>

</div>

</div>

</div>

</div>

</div>

How It Works?

You click “APPLY”.

submitalldata() runs → calls backend method GetClientSessionData().

The server sends back the current session user info (ClientFname, PAN, etc.).

JavaScript fills your modal table dynamically.

Bootstrap modal (#conformModalpopup) opens showing correct data.

No full postback — everything happens via AJAX. 

Windows Hosting Recommendation

HostForLIFE.eu receives Spotlight standing advantage award for providing recommended, cheap and fast ecommerce Hosting including the latest Magento. From the leading technology company, Microsoft. All the servers are equipped with the newest Windows Server 2022 R2, SQL Server 2022, ASP.NET Core 10.0 , ASP.NET MVC, Silverlight 5, WebMatrix and Visual Studio Lightswitch. Security and performance are at the core of their Magento hosting operations to confirm every website and/or application hosted on their servers is highly secured and performs at optimum level. mutually of the European ASP.NET hosting suppliers, HostForLIFE guarantees 99.9% uptime and fast loading speed. From €3.49/month , HostForLIFE provides you with unlimited disk space, unlimited domains, unlimited bandwidth,etc, for your website hosting needs.
 
https://hostforlifeasp.net/
Read More

Improving nopCommerce Efficiency: Useful Optimization Strategies for Fast Stores

Leave a Comment

Any eCommerce platform must have strong performance, particularly in terms of conversion rates, SEO rankings, and customer experience. Although nopCommerce 4.80.9, which is based on the robust ASP.NET Core framework, has a strong performance foundation by default, real-world stores frequently need tweaking to get blazingly fast page loads.



This post will discuss doable nopCommerce speed optimization strategies that you can put into practice immediately, ranging from frontend and server-level improvements to database tuning and caching.

1. Enable and Optimize Caching

nopCommerce uses a built-in caching mechanism to store frequently accessed data (such as settings, categories, products, and plugins).
However, the cache configuration can significantly impact performance.

What to Do

Ensure caching is enabled in appsettings.json

"DistributedCacheConfig": {
  "Enabled": true,
  "UseRedis": true,
  "RedisConnectionString": "localhost:6379"
}
  • Use Redis cache instead of in-memory cache when you’re running multiple instances (e.g., on Azure or AWS load balancing).

  • Avoid unnecessary cache invalidation. When writing plugins or custom logic, be careful not to clear global cache ( _cache.RemoveByPattern("*") ) unless absolutely required.

Pro Tip

Use ICacheKeyService to manage cache keys and dependency tags efficiently.

2. Optimize Database Performance

Database queries are often the bottleneck in nopCommerce applications.
Start by reviewing long-running SQL queries using tools like SQL Server Profiler or New Relic .

Optimization Techniques

  1. Add proper indexes to frequently used columns in tables like:

    • Product

    • Product_Category_Mapping

    • Order

    • Customer

    Example

    CREATE INDEX IX_Product_Published_Deleted ON Product (Published, Deleted)
  1. Avoid N+1 queries in custom LINQ or repository code.
    Use .Include() wisely when fetching related data.

  2. Use Stored Procedures for batch updates or heavy reporting.

  3. Enable Query Caching when using custom queries.

3. Minimize Plugin Overhead

nopCommerce’s plugin system is powerful — but too many plugins can increase startup time and memory consumption.

What to Do

  • Remove unused plugins from /Plugins folder.

  • Disable unnecessary plugins in Admin → Configuration → Local Plugins.

  • Avoid duplicate functionality.

  • When developing your own plugin, use async/await for all I/O operations and dispose of the DbContext properly.

4. Optimize Frontend (CSS, JS, and Images)

Frontend optimization often yields the most visible speed improvement.

Techniques

  1. Enable bundling and minification
    In Admin → Configuration → Settings → General Settings , ensure “Enable bundling and minification” is checked.

  2. Use modern image formats like WebP.

  3. Defer non-critical JavaScript and load analytics asynchronously.

  4. Leverage Cloudflare or CDN to serve static assets globally.

Example of async analytics loading

<script async src="https://www.googletagmanager.com/gtag/js?id=G-XXXXXXX"></script>

5. Tune Application Settings

Some configuration tweaks can dramatically reduce load time.

Recommendations

  • Set "UseResponseCompression": true in appsettings.json .

  • Use Kestrel instead of IIS in Linux-based deployments for better throughput.

  • Enable HTTP/2 support.

  • Configure Entity Framework connection pooling for efficient DB access.

Example snippet

builder.Services.AddDbContextPool<NopDbContext>(options =>
options.UseSqlServer(configuration.GetConnectionString("NopConnection")));

6. Use CDN and Caching Headers

To optimize delivery of static resources:

  • Configure Cache-Control headers for static content.

  • Integrate CDN (Cloudflare, Azure CDN, AWS CloudFront) for global delivery.

Example

app.UseStaticFiles(new StaticFileOptions
{
    OnPrepareResponse = ctx =>
    {
        ctx.Context.Response.Headers.Append("Cache-Control", "public,max-age=604800");
    }
});

7. Monitor and Measure

Use monitoring tools like:

  • New Relic or Application Insights for request tracking

  • MiniProfiler for local performance testing

  • SQL Server Profiler for database-level insights

Track slow pages, SQL queries, and memory usage — then optimize based on data, not guesswork.

8. Advanced: Background Task Tuning

nopCommerce runs background tasks like sending emails or syncing feeds.

Tip

  • Reduce frequency of non-critical background tasks.

  • Offload heavy operations to a separate worker service or queue system (e.g., Azure Queue or RabbitMQ).

Example

public class ProductFeedTask : IScheduleTask
{
    public Task ExecuteAsync()
    {
        // Move heavy work to background worker
        return Task.Run(() => _feedService.GenerateAsync());
    }
}

Read More

Entity Framework Tutorial: Set Up the Master Redux Toolkit in Just a Few Minutes!

Leave a Comment

If you've ever felt Redux setup was too verbose or complex, you're not alone. That's exactly why I created a concise, beginner-friendly guide to streamline your Redux Toolkit integration in React projects.

Steps for RTK setup
StepDescription
InstallInstall @reduxjs/toolkit and react-redux
SliceCreate a todoSlice for the todos logic
StoreConfigure the Redux store with the todo reducer
ProviderWrap the app with the Provider
ComponentsUse useSelector and useDispatch in components

1. Install Redux Toolkit & React-Redux

npm install @reduxjs/toolkit react-redux

2. Create the Todo Slice

Creating a slice requires a string name to identify the slice, an initial state value, and one or more reducer functions to define how the state can be updated. Once a slice is created, we can export the generated Redux action creators and the reducer function for the whole slice.

// src/redux/todoSlice.js
import { createSlice } from "@reduxjs/toolkit";

const todoSlice = createSlice({
  name: "todo",
  initialState: [],
  reducers: {
    addTodo: (state, action) => {
      state.push({
        id: Date.now(),
        text: action.payload,
        completed: false,
      });
    },
    updateTodo: (state, action) => {
      const { id, newText } = action.payload;
      const todo = state.find((todo) => todo.id === id);
      if (todo) {
        todo.text = newText;
      }
    },
    deleteTodo: (state, action) => {
      return state.filter((t) => t.id !== action.payload);
    },
  },
});
// Export actions and reducer
export const { addTodo, updateTodo, deleteTodo } = todoSlice.actions;
export default todoSlice.reducer;

3. Configure the Store

// src/redux/store.js
import { configureStore } from "@reduxjs/toolkit";
import todoReducer from "./todoSlice";

export const store = configureStore({
  reducer: {
    todo: todoReducer,
  },
});

4. Provide the Store to React

// src/index.js
import React from "react";
import ReactDOM from "react-dom/client";
import { Provider } from "react-redux";
import { store } from "./redux/store";
import App from "./App";

const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(
  <Provider store={store}>
    <App />
  </Provider>
);

RTK configuration is done, now you can use it in your features/components.

5. TodoInput Component

// src/components/TodoInput.js
import React, { useState } from "react";
import { useDispatch } from "react-redux";
import { addTodo } from "../redux/todoSlice";

function TodoInput() {
  const [text, setText] = useState("");
  const dispatch = useDispatch();

  const handleAdd = () => {
    if (text.trim()) {
      dispatch(addTodo(text));
      setText("");
    }
  };

  return (
    <div>
      <input
        value={text}
        onChange={e => setText(e.target.value)}
        placeholder="Enter todo"
      />
      <button onClick={handleAdd}>Add Todo</button>
    </div>
  );
}

export default TodoInput;

6. TodoList Component

// src/components/TodoList.js
import React from "react";
import { useSelector, useDispatch } from "react-redux";
import { updateTodo, deleteTodo } from "../redux/todoSlice";

function TodoList() {
  const todos = useSelector(state => state.todo);
  const dispatch = useDispatch();

  const handleUpdate = (id) => {
    const newText = prompt("Update todo text:");
    if (newText) {
      dispatch(updateTodo({ id, newText }));
    }
  };

  return (
    <ul>
      {todos.map(todo => (
        <li key={todo.id}>
          {todo.text}
          <button onClick={() => handleUpdate(todo.id)}>Update</button>
          <button onClick={() => dispatch(deleteTodo(todo.id))}>Delete</button>
        </li>
      ))}
    </ul>
  );
}

export default TodoList;
7. App Component
// src/App.js
import React from "react";
import TodoInput from "./components/TodoInput";
import TodoList from "./components/TodoList";

function App() {
  return (
    <div>
      <h2>Redux Toolkit Todo App</h2>
      <TodoInput />
      <TodoList />
    </div>
  );
}

export default App;

Windows Hosting Recommendation

HostForLIFEASP.NET receives Spotlight standing advantage award for providing recommended, cheap and fast ecommerce Hosting including the latest Magento. From the leading technology company, Microsoft. All the servers are equipped with the newest Windows Server 2022, SQL Server 2019, ASP.NET Core 10.0, ASP.NET MVC 6.0, Silverlight 5, WebMatrix and Visual Studio Lightswitch. Security and performance are at the core of their Magento hosting operations to confirm every website and/or application hosted on their servers is highly secured and performs at optimum level. mutually of the European ASP.NET hosting suppliers, HostForLIFE guarantees 99.9% uptime and fast loading speed. From €3.49/month , HostForLIFE provides you with unlimited disk space, unlimited domains, unlimited bandwidth,etc, for your website hosting needs.
 
https://hostforlifeasp.net/
Read More
Previous PostOlder Posts Home