{"id":955,"date":"2025-05-02T07:46:19","date_gmt":"2025-05-02T06:46:19","guid":{"rendered":"https:\/\/debuggersspace.com\/?p=955"},"modified":"2025-05-02T07:51:56","modified_gmt":"2025-05-02T06:51:56","slug":"understanding-startup-cs-appsettings-json-filters-and-using-keyword-in-net-core-with-real-world-examples","status":"publish","type":"post","link":"https:\/\/debuggersspace.com\/index.php\/2025\/05\/02\/understanding-startup-cs-appsettings-json-filters-and-using-keyword-in-net-core-with-real-world-examples\/","title":{"rendered":"Understanding Startup.cs, appsettings.json, Filters, and using Keyword in .NET Core \u2013 With Real-World Examples"},"content":{"rendered":"<div class='booster-block booster-read-block'>\n                <div class=\"twp-read-time\">\n                \t<i class=\"booster-icon twp-clock\"><\/i> <span>Read Time:<\/span>4 Minute, 14 Second                <\/div>\n\n            <\/div><h2 class=\"\" data-start=\"368\" data-end=\"405\">\ud83d\udccc 1. <code data-start=\"377\" data-end=\"389\">Startup.cs<\/code> in ASP.NET Core<\/h2>\n<h3 class=\"\" data-start=\"407\" data-end=\"427\">\u2705 <strong data-start=\"413\" data-end=\"427\">Definition<\/strong><\/h3>\n<p class=\"\" data-start=\"428\" data-end=\"571\"><code data-start=\"428\" data-end=\"440\">Startup.cs<\/code> is the central configuration file for your ASP.NET Core application, used to register services and define the middleware pipeline.<\/p>\n<h3 class=\"\" data-start=\"573\" data-end=\"601\">\ud83d\udccd <strong data-start=\"580\" data-end=\"601\">Where is it used?<\/strong><\/h3>\n<ul data-start=\"602\" data-end=\"726\">\n<li class=\"\" data-start=\"602\" data-end=\"656\">\n<p class=\"\" data-start=\"604\" data-end=\"656\">In every ASP.NET Core project (<code data-start=\"635\" data-end=\"643\">.NET 5<\/code> or earlier).<\/p>\n<\/li>\n<li class=\"\" data-start=\"657\" data-end=\"726\">\n<p class=\"\" data-start=\"659\" data-end=\"726\">Configures DI (Dependency Injection), routing, authentication, etc.<\/p>\n<\/li>\n<\/ul>\n<h3 class=\"\" data-start=\"728\" data-end=\"754\">\u2699\ufe0f <strong data-start=\"735\" data-end=\"754\">How is it used?<\/strong><\/h3>\n<ul data-start=\"755\" data-end=\"826\">\n<li class=\"\" data-start=\"755\" data-end=\"826\">\n<p class=\"\" data-start=\"757\" data-end=\"826\">By implementing two methods: <code data-start=\"786\" data-end=\"807\">ConfigureServices()<\/code> and <code data-start=\"812\" data-end=\"825\">Configure()<\/code>.<\/p>\n<\/li>\n<\/ul>\n<h3 class=\"\" data-start=\"828\" data-end=\"854\">\ud83c\udfaf <strong data-start=\"835\" data-end=\"854\">Why is it used?<\/strong><\/h3>\n<ul data-start=\"855\" data-end=\"933\">\n<li class=\"\" data-start=\"855\" data-end=\"933\">\n<p class=\"\" data-start=\"857\" data-end=\"933\">To define the behavior of the app at startup: dependencies, middleware, etc.<\/p>\n<\/li>\n<\/ul>\n<h3 class=\"\" data-start=\"935\" data-end=\"958\">\ud83d\udcbc <strong data-start=\"942\" data-end=\"958\">Code Example<\/strong><\/h3>\n<div class=\"contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary\">\n<div class=\"overflow-y-auto p-4\" dir=\"ltr\"><code class=\"whitespace-pre! language-csharp\"><code class=\"whitespace-pre! language-csharp\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Startup<\/span><br \/>\n{<br \/>\n<span class=\"hljs-keyword\">public<\/span> IConfiguration Configuration { <span class=\"hljs-keyword\">get<\/span>; }<\/code><\/code><span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span><\/span> <span class=\"hljs-title\">Startup<\/span>(<span class=\"hljs-params\">IConfiguration configuration<\/span>)<br \/>\n{<br \/>\nConfiguration = configuration;<br \/>\n}<\/p>\n<p><code class=\"whitespace-pre! language-csharp\"><code class=\"whitespace-pre! language-csharp\"><\/code><\/code><span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span><\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">ConfigureServices<\/span>(<span class=\"hljs-params\">IServiceCollection services<\/span>)<br \/>\n{<br \/>\nservices.AddControllers();<br \/>\nservices.AddScoped&lt;IMyService, MyService&gt;();<br \/>\n}<\/p>\n<p><code class=\"whitespace-pre! language-csharp\"><code class=\"whitespace-pre! language-csharp\"><\/code><\/code><span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span><\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">Configure<\/span>(<span class=\"hljs-params\">IApplicationBuilder app, IWebHostEnvironment env<\/span>)<br \/>\n{<br \/>\n<span class=\"hljs-keyword\">if<\/span> (env.IsDevelopment()) app.UseDeveloperExceptionPage();<\/p>\n<p><code class=\"whitespace-pre! language-csharp\"><code class=\"whitespace-pre! language-csharp\"><\/code><\/code>app.UseRouting();<br \/>\napp.UseAuthorization();<br \/>\napp.UseEndpoints(endpoints =&gt; endpoints.MapControllers());<br \/>\n}<br \/>\n}<\/p>\n<\/div>\n<\/div>\n<h3 class=\"\" data-start=\"1598\" data-end=\"1638\">\ud83c\udfa4 <strong data-start=\"1605\" data-end=\"1638\">Interview Questions &amp; Answers<\/strong><\/h3>\n<p class=\"\" data-start=\"1640\" data-end=\"1844\"><strong data-start=\"1640\" data-end=\"1715\">Q1: What is the difference between <code data-start=\"1677\" data-end=\"1696\">ConfigureServices<\/code> and <code data-start=\"1701\" data-end=\"1712\">Configure<\/code>?<\/strong><br data-start=\"1715\" data-end=\"1718\" \/>A: <code data-start=\"1721\" data-end=\"1740\">ConfigureServices<\/code> registers services for DI, while <code data-start=\"1774\" data-end=\"1785\">Configure<\/code> builds the middleware pipeline for handling HTTP requests.<\/p>\n<p class=\"\" data-start=\"1846\" data-end=\"2014\"><strong data-start=\"1846\" data-end=\"1894\">Q2: Can we have multiple <code data-start=\"1873\" data-end=\"1885\">Startup.cs<\/code> files?<\/strong><br data-start=\"1894\" data-end=\"1897\" \/>A: Yes, by using environment-specific Startup classes like <code data-start=\"1956\" data-end=\"1979\">StartupDevelopment.cs<\/code> and defining them in <code data-start=\"2001\" data-end=\"2013\">Program.cs<\/code>.<\/p>\n<p class=\"\" data-start=\"2016\" data-end=\"2152\"><strong data-start=\"2016\" data-end=\"2074\">Q3: How is <code data-start=\"2029\" data-end=\"2041\">Startup.cs<\/code> linked to the app&#8217;s lifecycle?<\/strong><br data-start=\"2074\" data-end=\"2077\" \/>A: It\u2019s automatically invoked during app bootstrapping by the host builder.<\/p>\n<hr class=\"\" data-start=\"2154\" data-end=\"2157\" \/>\n<h2 class=\"\" data-start=\"2159\" data-end=\"2211\">\ud83d\udcc2 2. <code data-start=\"2168\" data-end=\"2186\">appsettings.json<\/code> \u2013 The Configuration File<\/h2>\n<h3 class=\"\" data-start=\"2213\" data-end=\"2233\">\u2705 <strong data-start=\"2219\" data-end=\"2233\">Definition<\/strong><\/h3>\n<p class=\"\" data-start=\"2234\" data-end=\"2338\"><code data-start=\"2234\" data-end=\"2252\">appsettings.json<\/code> stores app-level configurations like DB connection strings, keys, and logging levels.<\/p>\n<h3 class=\"\" data-start=\"2340\" data-end=\"2368\">\ud83d\udccd <strong data-start=\"2347\" data-end=\"2368\">Where is it used?<\/strong><\/h3>\n<ul data-start=\"2369\" data-end=\"2452\">\n<li class=\"\" data-start=\"2369\" data-end=\"2419\">\n<p class=\"\" data-start=\"2371\" data-end=\"2419\">Found at the root of every ASP.NET Core project.<\/p>\n<\/li>\n<li class=\"\" data-start=\"2420\" data-end=\"2452\">\n<p class=\"\" data-start=\"2422\" data-end=\"2452\">Accessed via <code data-start=\"2435\" data-end=\"2451\">IConfiguration<\/code>.<\/p>\n<\/li>\n<\/ul>\n<h3 class=\"\" data-start=\"2454\" data-end=\"2480\">\u2699\ufe0f <strong data-start=\"2461\" data-end=\"2480\">How is it used?<\/strong><\/h3>\n<ul data-start=\"2481\" data-end=\"2562\">\n<li class=\"\" data-start=\"2481\" data-end=\"2562\">\n<p class=\"\" data-start=\"2483\" data-end=\"2562\">Values are injected using <code data-start=\"2509\" data-end=\"2531\">Configuration[\"key\"]<\/code> or via strongly typed objects.<\/p>\n<\/li>\n<\/ul>\n<h3 class=\"\" data-start=\"2564\" data-end=\"2590\">\ud83c\udfaf <strong data-start=\"2571\" data-end=\"2590\">Why is it used?<\/strong><\/h3>\n<ul data-start=\"2591\" data-end=\"2645\">\n<li class=\"\" data-start=\"2591\" data-end=\"2645\">\n<p class=\"\" data-start=\"2593\" data-end=\"2645\">To externalize and separate configuration from code.<\/p>\n<\/li>\n<\/ul>\n<h3 class=\"\" data-start=\"2647\" data-end=\"2670\">\ud83d\udcbc <strong data-start=\"2654\" data-end=\"2670\">Code Example<\/strong><\/h3>\n<div class=\"contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary\">\n<div class=\"overflow-y-auto p-4\" dir=\"ltr\"><code class=\"whitespace-pre! language-json\"><span class=\"hljs-punctuation\">{<\/span><br \/>\n<span class=\"hljs-attr\">\"ConnectionStrings\"<\/span><span class=\"hljs-punctuation\">:<\/span> <span class=\"hljs-punctuation\">{<\/span><br \/>\n<span class=\"hljs-attr\">\"DefaultConnection\"<\/span><span class=\"hljs-punctuation\">:<\/span> <span class=\"hljs-string\">\"Server=.;Database=MyDb;Trusted_Connection=True;\"<\/span><br \/>\n<span class=\"hljs-punctuation\">}<\/span><span class=\"hljs-punctuation\">,<\/span><br \/>\n<span class=\"hljs-attr\">\"Jwt\"<\/span><span class=\"hljs-punctuation\">:<\/span> <span class=\"hljs-punctuation\">{<\/span><br \/>\n<span class=\"hljs-attr\">\"Key\"<\/span><span class=\"hljs-punctuation\">:<\/span> <span class=\"hljs-string\">\"SecretKey123\"<\/span><span class=\"hljs-punctuation\">,<\/span><br \/>\n<span class=\"hljs-attr\">\"Issuer\"<\/span><span class=\"hljs-punctuation\">:<\/span> <span class=\"hljs-string\">\"MyApp\"<\/span><br \/>\n<span class=\"hljs-punctuation\">}<\/span><br \/>\n<span class=\"hljs-punctuation\">}<\/span><br \/>\n<\/code><\/div>\n<\/div>\n<div class=\"contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary\">\n<div class=\"flex items-center text-token-text-secondary px-4 py-2 text-xs font-sans justify-between h-9 bg-token-sidebar-surface-primary dark:bg-token-main-surface-secondary select-none rounded-t-[5px]\">csharp<\/div>\n<div class=\"overflow-y-auto p-4\" dir=\"ltr\"><code class=\"whitespace-pre! language-csharp\"><span class=\"hljs-comment\">\/\/ Reading from appsettings.json<\/span><br \/>\n<span class=\"hljs-built_in\">string<\/span> connStr = Configuration.GetConnectionString(<span class=\"hljs-string\">\"DefaultConnection\"<\/span>);<br \/>\n<span class=\"hljs-built_in\">string<\/span> jwtKey = Configuration[<span class=\"hljs-string\">\"Jwt:Key\"<\/span>];<br \/>\n<\/code><\/div>\n<\/div>\n<h3 class=\"\" data-start=\"3020\" data-end=\"3060\">\ud83c\udfa4 <strong data-start=\"3027\" data-end=\"3060\">Interview Questions &amp; Answers<\/strong><\/h3>\n<p class=\"\" data-start=\"3062\" data-end=\"3238\"><strong data-start=\"3062\" data-end=\"3124\">Q1: How do you access strongly typed configuration values?<\/strong><br data-start=\"3124\" data-end=\"3127\" \/>A: Use <code data-start=\"3134\" data-end=\"3198\">services.Configure&lt;T&gt;(Configuration.GetSection(\"SectionName\"))<\/code> and inject <code data-start=\"3210\" data-end=\"3223\">IOptions&lt;T&gt;<\/code> in your class.<\/p>\n<p class=\"\" data-start=\"3240\" data-end=\"3431\"><strong data-start=\"3240\" data-end=\"3307\">Q2: How do you manage multiple environments (dev\/staging\/prod)?<\/strong><br data-start=\"3307\" data-end=\"3310\" \/>A: By creating <code data-start=\"3325\" data-end=\"3355\">appsettings.Development.json<\/code>, <code data-start=\"3357\" data-end=\"3386\">appsettings.Production.json<\/code>, etc., and setting <code data-start=\"3406\" data-end=\"3430\">ASPNETCORE_ENVIRONMENT<\/code>.<\/p>\n<p class=\"\" data-start=\"3433\" data-end=\"3567\"><strong data-start=\"3433\" data-end=\"3474\">Q3: What happens if a key is missing?<\/strong><br data-start=\"3474\" data-end=\"3477\" \/>A: <code data-start=\"3480\" data-end=\"3509\">Configuration[\"missingKey\"]<\/code> returns <code data-start=\"3518\" data-end=\"3524\">null<\/code>\u2014handle null-checks or use fallback values.<\/p>\n<hr class=\"\" data-start=\"3569\" data-end=\"3572\" \/>\n<h2 class=\"\" data-start=\"3574\" data-end=\"3606\">\ud83e\uddf0 3. Filters in ASP.NET Core<\/h2>\n<h3 class=\"\" data-start=\"3608\" data-end=\"3628\">\u2705 <strong data-start=\"3614\" data-end=\"3628\">Definition<\/strong><\/h3>\n<p class=\"\" data-start=\"3629\" data-end=\"3750\">Filters allow you to execute logic at specific points during request processing, like before or after controller actions.<\/p>\n<h3 class=\"\" data-start=\"3752\" data-end=\"3783\">\ud83d\udccd <strong data-start=\"3759\" data-end=\"3783\">Where are they used?<\/strong><\/h3>\n<ul data-start=\"3784\" data-end=\"3920\">\n<li class=\"\" data-start=\"3784\" data-end=\"3827\">\n<p class=\"\" data-start=\"3786\" data-end=\"3827\">Global level: applied to all controllers.<\/p>\n<\/li>\n<li class=\"\" data-start=\"3828\" data-end=\"3863\">\n<p class=\"\" data-start=\"3830\" data-end=\"3863\">Controller level or action level.<\/p>\n<\/li>\n<li class=\"\" data-start=\"3864\" data-end=\"3920\">\n<p class=\"\" data-start=\"3866\" data-end=\"3920\">Common in logging, authentication, and error handling.<\/p>\n<\/li>\n<\/ul>\n<h3 class=\"\" data-start=\"3922\" data-end=\"3951\">\u2699\ufe0f <strong data-start=\"3929\" data-end=\"3951\">How are they used?<\/strong><\/h3>\n<ul data-start=\"3952\" data-end=\"4036\">\n<li class=\"\" data-start=\"3952\" data-end=\"4036\">\n<p class=\"\" data-start=\"3954\" data-end=\"4036\">By implementing <code data-start=\"3970\" data-end=\"3985\">IActionFilter<\/code>, <code data-start=\"3987\" data-end=\"4005\">IExceptionFilter<\/code>, or using built-in attributes.<\/p>\n<\/li>\n<\/ul>\n<h3 class=\"\" data-start=\"4038\" data-end=\"4067\">\ud83c\udfaf <strong data-start=\"4045\" data-end=\"4067\">Why are they used?<\/strong><\/h3>\n<ul data-start=\"4068\" data-end=\"4143\">\n<li class=\"\" data-start=\"4068\" data-end=\"4143\">\n<p class=\"\" data-start=\"4070\" data-end=\"4143\">To separate concerns like logging and error handling from business logic.<\/p>\n<\/li>\n<\/ul>\n<h3 class=\"\" data-start=\"4145\" data-end=\"4168\">\ud83d\udcbc <strong data-start=\"4152\" data-end=\"4168\">Code Example<\/strong><\/h3>\n<div class=\"contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary\">\n<div class=\"overflow-y-auto p-4\" dir=\"ltr\"><code class=\"whitespace-pre! language-csharp\"><code class=\"whitespace-pre! language-csharp\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">LogActionFilter<\/span> : <span class=\"hljs-title\">IActionFilter<\/span><br \/>\n{<br \/>\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span><\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">OnActionExecuting<\/span>(<span class=\"hljs-params\">ActionExecutingContext context<\/span>)<br \/>\n{<br \/>\nConsole.WriteLine(<span class=\"hljs-string\">\"Action started\"<\/span>);<br \/>\n}<\/code><\/code><span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span><\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">OnActionExecuted<\/span>(<span class=\"hljs-params\">ActionExecutedContext context<\/span>)<br \/>\n{<br \/>\nConsole.WriteLine(<span class=\"hljs-string\">&#8220;Action ended&#8221;<\/span>);<br \/>\n}<br \/>\n}<\/p>\n<\/div>\n<\/div>\n<div class=\"contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary\">\n<div class=\"overflow-y-auto p-4\" dir=\"ltr\"><code class=\"whitespace-pre! language-csharp\"><span class=\"hljs-comment\">\/\/ Registering the filter globally<\/span><br \/>\nservices.AddControllers(options =&gt; {<br \/>\noptions.Filters.Add&lt;LogActionFilter&gt;();<br \/>\n});<br \/>\n<\/code><\/div>\n<\/div>\n<h3 class=\"\" data-start=\"4611\" data-end=\"4651\">\ud83c\udfa4 <strong data-start=\"4618\" data-end=\"4651\">Interview Questions &amp; Answers<\/strong><\/h3>\n<p class=\"\" data-start=\"4653\" data-end=\"4834\"><strong data-start=\"4653\" data-end=\"4715\">Q1: What is the difference between middleware and filters?<\/strong><br data-start=\"4715\" data-end=\"4718\" \/>A: Middleware applies globally and works outside MVC, while filters are scoped to MVC and can access action context.<\/p>\n<p class=\"\" data-start=\"4836\" data-end=\"4913\"><strong data-start=\"4836\" data-end=\"4865\">Q2: Can filters be async?<\/strong><br data-start=\"4865\" data-end=\"4868\" \/>A: Yes, by implementing <code data-start=\"4892\" data-end=\"4912\">IAsyncActionFilter<\/code>.<\/p>\n<p class=\"\" data-start=\"4915\" data-end=\"5053\"><strong data-start=\"4915\" data-end=\"4959\">Q3: When to use a filter vs. middleware?<\/strong><br data-start=\"4959\" data-end=\"4962\" \/>A: Use filters when you need access to <code data-start=\"5001\" data-end=\"5013\">ModelState<\/code>, action parameters, or result metadata.<\/p>\n<hr class=\"\" data-start=\"5055\" data-end=\"5058\" \/>\n<h2 class=\"\" data-start=\"5060\" data-end=\"5094\">\ud83e\uddfe 4. The <code data-start=\"5073\" data-end=\"5080\">using<\/code> Keyword in C#<\/h2>\n<h3 class=\"\" data-start=\"5096\" data-end=\"5116\">\u2705 <strong data-start=\"5102\" data-end=\"5116\">Definition<\/strong><\/h3>\n<p class=\"\" data-start=\"5117\" data-end=\"5137\"><code data-start=\"5117\" data-end=\"5124\">using<\/code> is used for:<\/p>\n<ol data-start=\"5138\" data-end=\"5214\">\n<li class=\"\" data-start=\"5138\" data-end=\"5162\">\n<p class=\"\" data-start=\"5141\" data-end=\"5162\">Importing namespaces.<\/p>\n<\/li>\n<li class=\"\" data-start=\"5163\" data-end=\"5214\">\n<p class=\"\" data-start=\"5166\" data-end=\"5214\">Managing resources that implement <code data-start=\"5200\" data-end=\"5213\">IDisposable<\/code>.<\/p>\n<\/li>\n<\/ol>\n<h3 class=\"\" data-start=\"5216\" data-end=\"5244\">\ud83d\udccd <strong data-start=\"5223\" data-end=\"5244\">Where is it used?<\/strong><\/h3>\n<ul data-start=\"5245\" data-end=\"5382\">\n<li class=\"\" data-start=\"5245\" data-end=\"5292\">\n<p class=\"\" data-start=\"5247\" data-end=\"5292\">Every C# file starts with <code data-start=\"5273\" data-end=\"5280\">using<\/code> directives.<\/p>\n<\/li>\n<li class=\"\" data-start=\"5293\" data-end=\"5382\">\n<p class=\"\" data-start=\"5295\" data-end=\"5382\"><code data-start=\"5295\" data-end=\"5302\">using<\/code> statements are used for disposable resources like DB connections, file streams.<\/p>\n<\/li>\n<\/ul>\n<h3 class=\"\" data-start=\"5384\" data-end=\"5410\">\u2699\ufe0f <strong data-start=\"5391\" data-end=\"5410\">How is it used?<\/strong><\/h3>\n<ul data-start=\"5411\" data-end=\"5528\">\n<li class=\"\" data-start=\"5411\" data-end=\"5490\">\n<p class=\"\" data-start=\"5413\" data-end=\"5490\">Namespace imports allow usage of classes like <code data-start=\"5459\" data-end=\"5471\">HttpClient<\/code>, <code data-start=\"5473\" data-end=\"5484\">DbContext<\/code>, etc.<\/p>\n<\/li>\n<li class=\"\" data-start=\"5491\" data-end=\"5528\">\n<p class=\"\" data-start=\"5493\" data-end=\"5528\">Statement ensures resource cleanup.<\/p>\n<\/li>\n<\/ul>\n<h3 class=\"\" data-start=\"5530\" data-end=\"5556\">\ud83c\udfaf <strong data-start=\"5537\" data-end=\"5556\">Why is it used?<\/strong><\/h3>\n<ul data-start=\"5557\" data-end=\"5654\">\n<li class=\"\" data-start=\"5557\" data-end=\"5609\">\n<p class=\"\" data-start=\"5559\" data-end=\"5609\">Prevents memory leaks by auto-disposing resources.<\/p>\n<\/li>\n<li class=\"\" data-start=\"5610\" data-end=\"5654\">\n<p class=\"\" data-start=\"5612\" data-end=\"5654\">Organizes code with external dependencies.<\/p>\n<\/li>\n<\/ul>\n<h3 class=\"\" data-start=\"5656\" data-end=\"5679\">\ud83d\udcbc <strong data-start=\"5663\" data-end=\"5679\">Code Example<\/strong><\/h3>\n<div class=\"contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary\">\n<div class=\"overflow-y-auto p-4\" dir=\"ltr\"><code class=\"whitespace-pre! language-csharp\"><code class=\"whitespace-pre! language-csharp\"><span class=\"hljs-comment\">\/\/ 1. Namespace import<\/span><br \/>\n<span class=\"hljs-keyword\">using<\/span> System.Net.Http;<\/code><\/code><span class=\"hljs-comment\">\/\/ 2. Resource disposal<\/span><br \/>\n<span class=\"hljs-keyword\">using<\/span> (<span class=\"hljs-keyword\">var<\/span> client = <span class=\"hljs-keyword\">new<\/span> HttpClient())<br \/>\n{<br \/>\n<span class=\"hljs-keyword\">var<\/span> response = <span class=\"hljs-keyword\">await<\/span> client.GetStringAsync(<span class=\"hljs-string\">&#8220;https:\/\/example.com&#8221;<\/span>);<br \/>\n}<\/p>\n<\/div>\n<\/div>\n<div class=\"contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary\">\n<div class=\"overflow-y-auto p-4\" dir=\"ltr\"><code class=\"whitespace-pre! language-csharp\"><span class=\"hljs-comment\">\/\/ Modern using declaration (C# 8+)<\/span><br \/>\n<span class=\"hljs-keyword\">using<\/span> <span class=\"hljs-keyword\">var<\/span> file = <span class=\"hljs-keyword\">new<\/span> FileStream(<span class=\"hljs-string\">\"log.txt\"<\/span>, FileMode.OpenOrCreate);<br \/>\n<\/code><\/div>\n<\/div>\n<h3 class=\"\" data-start=\"5997\" data-end=\"6037\">\ud83c\udfa4 <strong data-start=\"6004\" data-end=\"6037\">Interview Questions &amp; Answers<\/strong><\/h3>\n<p class=\"\" data-start=\"6039\" data-end=\"6181\"><strong data-start=\"6039\" data-end=\"6109\">Q1: What&#8217;s the difference between <code data-start=\"6075\" data-end=\"6082\">using<\/code> directive and statement?<\/strong><br data-start=\"6109\" data-end=\"6112\" \/>A: Directive brings in namespaces; statement ensures object disposal.<\/p>\n<p class=\"\" data-start=\"6183\" data-end=\"6341\"><strong data-start=\"6183\" data-end=\"6248\">Q2: What happens if you don\u2019t use <code data-start=\"6219\" data-end=\"6226\">using<\/code> with <code data-start=\"6232\" data-end=\"6245\">IDisposable<\/code>?<\/strong><br data-start=\"6248\" data-end=\"6251\" \/>A: The resource may stay in memory longer than needed, causing memory leaks or file locks.<\/p>\n<p class=\"\" data-start=\"6343\" data-end=\"6481\"><strong data-start=\"6343\" data-end=\"6389\">Q3: How is <code data-start=\"6356\" data-end=\"6363\">using<\/code> internally implemented?<\/strong><br data-start=\"6389\" data-end=\"6392\" \/>A: It&#8217;s translated into a try-finally block where <code data-start=\"6442\" data-end=\"6453\">Dispose()<\/code> is called in the <code data-start=\"6471\" data-end=\"6480\">finally<\/code>.<\/p>\n<hr class=\"\" data-start=\"6483\" data-end=\"6486\" \/>\n<h2 class=\"\" data-start=\"6488\" data-end=\"6511\">\ud83e\udde0 Quick Recap Table<\/h2>\n<div class=\"_tableContainer_16hzy_1\">\n<div class=\"_tableWrapper_16hzy_14 group flex w-fit flex-col-reverse\" tabindex=\"-1\">\n<table class=\"w-fit min-w-(--thread-content-width)\" data-start=\"6513\" data-end=\"7410\">\n<thead data-start=\"6513\" data-end=\"6661\">\n<tr data-start=\"6513\" data-end=\"6661\">\n<th data-start=\"6513\" data-end=\"6531\" data-col-size=\"sm\">Concept<\/th>\n<th data-start=\"6531\" data-end=\"6570\" data-col-size=\"sm\">Where Used<\/th>\n<th data-start=\"6570\" data-end=\"6617\" data-col-size=\"md\">How Used<\/th>\n<th data-start=\"6617\" data-end=\"6661\" data-col-size=\"sm\">Why Used<\/th>\n<\/tr>\n<\/thead>\n<tbody data-start=\"6811\" data-end=\"7410\">\n<tr data-start=\"6811\" data-end=\"6960\">\n<td data-start=\"6811\" data-end=\"6829\" data-col-size=\"sm\"><code data-start=\"6813\" data-end=\"6825\">Startup.cs<\/code><\/td>\n<td data-start=\"6829\" data-end=\"6869\" data-col-size=\"sm\">Entry of ASP.NET Core project<\/td>\n<td data-start=\"6869\" data-end=\"6916\" data-col-size=\"md\">Register services, build middleware pipeline<\/td>\n<td data-start=\"6916\" data-end=\"6960\" data-col-size=\"sm\">App bootstrapping and lifecycle config<\/td>\n<\/tr>\n<tr data-start=\"6961\" data-end=\"7110\">\n<td data-start=\"6961\" data-end=\"6982\" data-col-size=\"sm\"><code data-start=\"6963\" data-end=\"6981\">appsettings.json<\/code><\/td>\n<td data-start=\"6982\" data-end=\"7019\" data-col-size=\"sm\">Root configuration file<\/td>\n<td data-start=\"7019\" data-end=\"7066\" data-col-size=\"md\">Read via <code data-start=\"7030\" data-end=\"7052\">Configuration[\"key\"]<\/code><\/td>\n<td data-start=\"7066\" data-end=\"7110\" data-col-size=\"sm\">Manage environments and external config<\/td>\n<\/tr>\n<tr data-start=\"7111\" data-end=\"7260\">\n<td data-start=\"7111\" data-end=\"7129\" data-col-size=\"sm\">Filters<\/td>\n<td data-start=\"7129\" data-end=\"7169\" data-col-size=\"sm\">Global, Controller, or Action level<\/td>\n<td data-start=\"7169\" data-end=\"7216\" data-col-size=\"md\">Implement interfaces or use attributes<\/td>\n<td data-start=\"7216\" data-end=\"7260\" data-col-size=\"sm\">Decouple logging, validation, auth logic<\/td>\n<\/tr>\n<tr data-start=\"7261\" data-end=\"7410\">\n<td data-start=\"7261\" data-end=\"7279\" data-col-size=\"sm\"><code data-start=\"7263\" data-end=\"7270\">using<\/code><\/td>\n<td data-start=\"7279\" data-end=\"7319\" data-col-size=\"sm\">Top of file \/ disposable object scope<\/td>\n<td data-start=\"7319\" data-end=\"7366\" data-col-size=\"md\">Namespace import and resource management<\/td>\n<td data-start=\"7366\" data-end=\"7410\" data-col-size=\"sm\">Avoid memory leaks, improve readability<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"sticky end-(--thread-content-margin) h-0 self-end select-none\">\n<div class=\"absolute end-0 flex items-end\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<hr class=\"\" data-start=\"7412\" data-end=\"7415\" \/>\n<h2 class=\"\" data-start=\"7417\" data-end=\"7433\">\ud83d\udce3 Conclusion<\/h2>\n<p class=\"\" data-start=\"7435\" data-end=\"7696\">Understanding how <code data-start=\"7453\" data-end=\"7465\">Startup.cs<\/code>, <code data-start=\"7467\" data-end=\"7485\">appsettings.json<\/code>, Filters, and <code data-start=\"7500\" data-end=\"7507\">using<\/code> work together gives you a strong foundation in ASP.NET Core development. These are the hidden pillars that bring performance, maintainability, and structure to your enterprise application.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\ud83d\udccc 1. Startup.cs in ASP.NET Core \u2705 Definition Startup.cs is the central configuration file for your ASP.NET Core application, used to register services and define the middleware pipeline. \ud83d\udccd Where is it used? In every ASP.NET Core project (.NET 5 or earlier). Configures DI (Dependency Injection), routing, authentication, etc. \u2699\ufe0f How is it used? By [&hellip;]<\/p>\n","protected":false},"author":43,"featured_media":959,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_uag_custom_page_level_css":"","footnotes":""},"categories":[15,159,146,1,24],"tags":[],"class_list":["post-955","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-net-core-3-1","category-net-core-top-50-qa","category-net-interview-qa","category-blog","category-c-net"],"uagb_featured_image_src":{"full":["https:\/\/debuggersspace.com\/wp-content\/uploads\/2025\/05\/ChatGPT-Image-May-2-2025-12_20_58-PM.png",1536,1024,false],"thumbnail":["https:\/\/debuggersspace.com\/wp-content\/uploads\/2025\/05\/ChatGPT-Image-May-2-2025-12_20_58-PM-150x150.png",150,150,true],"medium":["https:\/\/debuggersspace.com\/wp-content\/uploads\/2025\/05\/ChatGPT-Image-May-2-2025-12_20_58-PM-300x200.png",300,200,true],"medium_large":["https:\/\/debuggersspace.com\/wp-content\/uploads\/2025\/05\/ChatGPT-Image-May-2-2025-12_20_58-PM-768x512.png",750,500,true],"large":["https:\/\/debuggersspace.com\/wp-content\/uploads\/2025\/05\/ChatGPT-Image-May-2-2025-12_20_58-PM-1024x683.png",750,500,true],"1536x1536":["https:\/\/debuggersspace.com\/wp-content\/uploads\/2025\/05\/ChatGPT-Image-May-2-2025-12_20_58-PM.png",1536,1024,false],"2048x2048":["https:\/\/debuggersspace.com\/wp-content\/uploads\/2025\/05\/ChatGPT-Image-May-2-2025-12_20_58-PM.png",1536,1024,false]},"uagb_author_info":{"display_name":"Himanshu Namdeo","author_link":"https:\/\/debuggersspace.com\/author\/admin\/"},"uagb_comment_info":0,"uagb_excerpt":"\ud83d\udccc 1. Startup.cs in ASP.NET Core \u2705 Definition Startup.cs is the central configuration file for your ASP.NET Core application, used to register services and define the middleware pipeline. \ud83d\udccd Where is it used? In every ASP.NET Core project (.NET 5 or earlier). Configures DI (Dependency Injection), routing, authentication, etc. \u2699\ufe0f How is it used? By&hellip;","_links":{"self":[{"href":"https:\/\/debuggersspace.com\/index.php\/wp-json\/wp\/v2\/posts\/955"}],"collection":[{"href":"https:\/\/debuggersspace.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/debuggersspace.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/debuggersspace.com\/index.php\/wp-json\/wp\/v2\/users\/43"}],"replies":[{"embeddable":true,"href":"https:\/\/debuggersspace.com\/index.php\/wp-json\/wp\/v2\/comments?post=955"}],"version-history":[{"count":4,"href":"https:\/\/debuggersspace.com\/index.php\/wp-json\/wp\/v2\/posts\/955\/revisions"}],"predecessor-version":[{"id":958,"href":"https:\/\/debuggersspace.com\/index.php\/wp-json\/wp\/v2\/posts\/955\/revisions\/958"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/debuggersspace.com\/index.php\/wp-json\/wp\/v2\/media\/959"}],"wp:attachment":[{"href":"https:\/\/debuggersspace.com\/index.php\/wp-json\/wp\/v2\/media?parent=955"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/debuggersspace.com\/index.php\/wp-json\/wp\/v2\/categories?post=955"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/debuggersspace.com\/index.php\/wp-json\/wp\/v2\/tags?post=955"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}