Connection Controller
using System.Web.Http;using eQCsomWebsite.Models;using eQCsomWebsite.Services;using eQCsomWebsite.Services.implementer;using System.Web.Services;namespace eQCsomWebsite.Controllers{ [RoutePrefix("api/connection")] public class ConnectionController : ApiController { private readonly IConnectionService _connectionService; // Constructor public ConnectionController() { // Manually resolving dependencies for .NET Framework 4.8 (e.g., via Unity, Ninject, or another DI container) _connectionService = new ConnectionService(); // You can use a DI container here } // POST api/connection/logIn [HttpPost] [Route("logIn")] public IHttpActionResult LogIn([FromBody] LogInRequest request) { if (request == null || string.IsNullOrEmpty(request.Username) || string.IsNullOrEmpty(request.Password)) { return BadRequest("Invalid login parameters."); } var response = _connectionService.LogIn(request); if (response == null || !response.Success) { return Unauthorized(); } return Ok(response); } // POST api/connection/logOff [HttpPost] [Route("logOff")] public IHttpActionResult LogOff([FromBody] LogOffRequest request) { if (request == null || string.IsNullOrEmpty(request.Token)) { return BadRequest("Token is required for logout."); } var response = _connectionService.LogOff(request); if (response == null || !response.Success) { return Unauthorized(); } return Ok(response); } }}
Global.asax.cs
using Unity;using Unity.WebApi;using System.Web.Http;using System.Web.Mvc;using eQCsomWebsite.Services;using eQCsomWebsite.Services.implementer;namespace eQCsomWebsite{ public class CsomApplication : System.Web.HttpApplication { protected void Application_Start() { // Initialize Unity container var container = new UnityContainer(); // Register dependencies container.RegisterType<IConnectionService, ConnectionService>(); container.RegisterType<IMetadataService, MetadataService>(); container.RegisterType<IOperationService, OperationService>(); // Set Web API Dependency Resolver to Unity GlobalConfiguration.Configuration.DependencyResolver = new UnityDependencyResolver(container); // Register Web API configuration WebApiConfig.Register(GlobalConfiguration.Configuration); // Register MVC routes (if using MVC) AreaRegistration.RegisterAllAreas(); RouteConfig.RegisterRoutes(System.Web.Routing.RouteTable.Routes); } }}
RouteConfig.cs
using System.Web.Mvc;using System.Web.Routing;using System.Web.Http;namespace eQCsomWebsite{ public class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { // Ignore routes for certain file types routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); // Add MVC routing (for normal MVC controllers) // routes.MapRoute( // name: "Default", // url: "{controller}/{action}/{id}", // defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } // ); // Add Web API routing GlobalConfiguration.Configuration.MapHttpAttributeRoutes(); } }}
<configuration><runtime><assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"><dependentAssembly><assemblyIdentity name="System.Web.Http" publicKeyToken="31bf3856ad364e35" culture="neutral" /><bindingRedirect oldVersion="0.0.0.0-5.2.7.0" newVersion="5.2.7.0" /></dependentAssembly><dependentAssembly><assemblyIdentity name="System.Web.Http.WebHost" publicKeyToken="31bf3856ad364e35" culture="neutral" /><bindingRedirect oldVersion="0.0.0.0-5.2.7.0" newVersion="5.2.7.0" /></dependentAssembly></assemblyBinding></runtime><system.webServer><!-- Registering Web API Handler --><handlers><add name="WebApiHandler" path="api/*" verb="*" type="System.Web.Http.WebHost.HttpControllerHandler, System.Web.Http.WebHost" resourceType="Unspecified" /></handlers><!-- Registering Web API Module --><modules><add name="WebApiModule" type="System.Web.Http.WebHost.HttpModule, System.Web.Http.WebHost" /></modules><!-- URL Rewrite for Web API routing --><rewrite><rules><rule name="WebAPI Routing"><match url="^api/(.*)" /><action type="Rewrite" url="/api/{R:1}" /></rule></rules></rewrite></system.webServer><!-- Optional: Configuration for HTTP request logging and diagnostics --><system.diagnostics><sources><source name="System.Web" switchValue="Information, ActivityTracing"><listeners><add name="WebApiDiagnostics" /></listeners></source></sources></system.diagnostics></configuration>
This API is not workingI am start server in vs code and Using iis but not able to hit end point
WebApiConfig.cs
using System.Web.Http;namespace eQCsomWebsite{ public static class WebApiConfig { public static void Register(HttpConfiguration config) { // Web API route configuration // Use attribute routing (this will automatically use [Route] and [RoutePrefix] attributes in controllers) config.MapHttpAttributeRoutes(); // Optional: You can also define a default route pattern if needed config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); } }}
I am not able to hit this post api for login or logout using postmanUrl:http://localhost:32578/api/connection/logInin authentication tab i have selected basic authI am start the VS code debugging as well as IIS Server but nothing is working for me