ASP.NET Core API बनाना: Minimal API, Controller और Middleware चुनें
ASP.NET Core API बनाते समय पहले Minimal API या controller चुनें, फिर service registration और middleware क्रम तय करें। सरल HTTP API के लिए Minimal API कम कोड से शुरू होती है; MVC सुविधाओं, जटिल संगठन या टीम की स्थापित परंपराओं के लिए controller उपयुक्त हो सकता है। Pipeline का क्रम और DI lifetime व्यवहार व परीक्षण बदलते हैं।
ASP.NET Core API बनाते समय पहले Minimal API या controller चुनें, फिर service registration और middleware क्रम तय करें। सरल HTTP API के लिए Minimal API कम कोड से शुरू होती है; MVC सुविधाओं, जटिल संगठन या टीम की स्थापित परंपराओं के लिए controller उपयुक्त हो सकता है। Pipeline का क्रम और DI lifetime व्यवहार व परीक्षण बदलते हैं।
यह मार्गदर्शिका ASP.NET Core 10.0 दस्तावेज़ों पर आधारित है। उदाहरण संरचना दिखाते हैं, संपूर्ण authentication, persistence या deployment configuration नहीं।
API शैली चुनें
Minimal API, Program.cs में route और handler सीधे map करती है। Controller, ControllerBase से निकली classes में endpoints समूहित करता है। साझा सुविधाओं और जिम्मेदारियों के आधार पर चुनें: कुछ स्वतंत्र routes Minimal API में स्पष्ट रह सकते हैं; साझा MVC सुविधाएँ या filters controllers के पक्ष में हो सकते हैं। दोनों शैलियाँ मिल सकती हैं, पर routes और policies के स्वामित्व के नियम तय करें।
सेवाएँ पंजीकृत कर inject करें
Implementation को builder.Services में register करें और constructor या handler parameter से प्राप्त करें। Built-in container सेवाएँ बनाता और dispose करता है। Lifetime सोचकर चुनें: Singleton पूरे app में साझा होता है और request state न रखे; Scoped अक्सर प्रति-request काम के लिए उपयुक्त है; Transient अनुरोध पर नया instance बनाता है। Singleton में Scoped service को पकड़े रहने से बचें।
Middleware क्रम समझें
Middleware अगले component से पहले और बाद में काम कर सकता है। Use आगे बढ़ाता है; Run pipeline समाप्त करता है। बाद की त्रुटियाँ पकड़ने के लिए exception handling सामान्यतः पहले रखें। HTTPS redirection, routing, authentication, authorization और endpoint mapping का documented क्रम अपनाएँ।
वातावरण की settings अलग रखें
ASP.NET Core providers appsettings.json, environment variables, command-line arguments और secret stores पढ़ते हैं। Development और deployment settings अलग रखें। Password और token repository में commit न करें; secret management से दें। अनपेक्षित मान पर active environment, providers, priority, content root और working directory देखें। Secrets log न करें।
महत्वपूर्ण integration paths जाँचें
Unit tests अलग logic को जल्दी जाँचते हैं। Integration tests test host और client से pipeline तथा database या files जैसी infrastructure को परखते हैं। महत्वपूर्ण read/write flows और boundaries पर ध्यान दें; सामान्य branches के लिए unit tests रखें। Test settings प्रयोग करें, data isolate और साफ़ करें, और response contract जाँचें।
क्रम से जाँचें: route और HTTP method; middleware क्रम और terminal Run; DI registrations और lifetimes; environment और providers। फिर धीमे integration tests सीमित करें और cleanup सुधारें।
स्रोत: API, dependency injection, middleware, configuration और integration testing पर Microsoft Learn ASP.NET Core 10.0 के आधिकारिक दस्तावेज़।
References
[ASP.NET Core 10.0: APIs overview](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/apis?view=aspnetcore-10.0)
[ASP.NET Core 10.0: Dependency injection](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-10.0)
[ASP.NET Core 10.0: Middleware](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/middleware/?view=aspnetcore-10.0)
[ASP.NET Core 10.0: Configuration](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-10.0)
[ASP.NET Core 10.0: Integration tests](https://learn.microsoft.com/en-us/aspnet/core/test/integration-tests?view=aspnetcore-10.0)