niketa yadav

niketa yadav

  • NA
  • 96
  • 895

Accessing Web API after hosting asp.net website

Aug 6 2020 6:28 AM
I have written a simple web API in the existing asp.net webforms project and published it on HostGator shared hosting.
 
API was working fine locally but not accessible after hosting.
 
Process how I created this API in My web forms project.
 
Step 1. I added a new Web API controller class to my Webform project by
             right-click on webform project ->Add->Web API Controller class.
 
Step 2 .Added some routing information to Application_Start () methof of global.asax file .
  1. GlobalConfiguration.Configure(WebApiConfig.Register);  
  2. public static class WebApiConfig  
  3. {  
  4. public static void Register(HttpConfiguration config)  
  5. {  
  6. // Web API configuration and services  
  7. // Web API routes  
  8. config.MapHttpAttributeRoutes();  
  9. config.Routes.MapHttpRoute(  
  10. name: "DefaultApi",  
  11. routeTemplate: "api/{controller}/{id}",  
  12. defaults: new { action = "GetRadarData", id = System.Web.Http.RouteParameter.Optional }  
  13. );  
  14. }  
  15. }  
Step 3: Adding code to the controller class for Get method.
  1. public IEnumerable<clsRadarInfoEnt> GetRadarData(string userId, string password)  
  2. {  
  3. string Response = "";  
  4. clsLibrary.mGetRadarList(userId, password, out RadarList, out Response);  
  5. if (RadarList == null && Response == "Invalid login")  
  6. {  
  7. var response = new HttpResponseMessage(HttpStatusCode.NotFound)  
  8. {  
  9. Content = new StringContent("User doesn't exist", System.Text.Encoding.UTF8, "text/plain"),  
  10. StatusCode = HttpStatusCode.NotFound  
  11. };  
  12. throw new HttpResponseException(response);  
  13. }  
  14. else if (RadarList == null && Response == "Incorrect password")  
  15. {  
  16. var response = new HttpResponseMessage(HttpStatusCode.Unauthorized)  
  17. {  
  18. Content = new StringContent("Incorrect Password", System.Text.Encoding.UTF8, "text/plain"),  
  19. StatusCode = HttpStatusCode.NotFound  
  20. };  
  21. throw new HttpResponseException(response);  
  22. }  
  23. else  
  24. {  
  25. return RadarList;  
  26. }  
  27. }  
It was working fine locally and giving the result as aspected but after hosting the website on Hostgator showing error
 
"The resource you are looking for has been removed, had its name changed, or is temporarily unavailable."
 
Please reply back.
 
Thank you in advance

Answers (4)