A set of technologies in .NET for building web applications and web services. Miscellaneous topics that do not fit into specific categories.
Hi @Olivia Jones ,
Thanks for sharing the details of your project. I will address the three areas you asked about:
1. Structuring the calculation logic You have a few options depending on how often tariffs change and who maintains them:
- Hardcoded in C# – simplest to start with, good if tariffs rarely change and only developers update them.
- Stored in
appsettings.json– still simple, allows updating tariff slabs without recompiling, but requires an app restart after edits. - Stored in a database (SQL Server) – best if tariffs change frequently or need to be updated by non-developers (e.g., via an admin UI).
For a quick prototype, hardcoding the slabs in a service class is fine. If this is going into production where tariffs may change, a database-driven approach is more maintainable.
2. Exposing the calculation to the frontend The recommended way in ASP.NET Core is to create an API endpoint that accepts the unit count and returns the calculated bill as JSON.
- Your frontend (JavaScript/jQuery) can call this endpoint using
fetchor AJAX. - This lets you update the page dynamically without a full reload.
- Example:
- Endpoint:
/api/bill/calculate?units=150 - Response:
{ "amount": 300 } - JavaScript uses the response to update the DOM.
- Endpoint:
This keeps the frontend lightweight and the calculation logic centralized on the backend.
You can check this guide out for how to create call an ASP.NET Core web API with JavaScript: Tutorial: Call an ASP.NET Core web API with JavaScript
3. Generating a downloadable PDF A common approach is to:
- Render the bill content (units, amount, etc.) into HTML or a simple document object.
- Pass that to a PDF library that can output a proper PDF.
- Return the PDF file from an endpoint so the browser can download it.
Libraries you can consider:
- QuestPDF (C# fluent API for building documents, modern and actively maintained).
- DinkToPdf (wrapper around wkhtmltopdf, converts HTML directly to PDF).
Both are widely used in ASP.NET Core apps.
Here's a small sample on how to implement QuestPDF in ASP.NET projects Integration with ASP.NET
Hope this helps, feel free to reach out if you need any clarification.