4
Answers

Accessing Web API after hosting asp.net website

Photo of niketa yadav

niketa yadav

4y
955
1
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)

1
Photo of Laxmidhar Sahoo
NA 10.4k 55.7k 4y
Hi
 
By default it is get method
 
Thanks and regards 
 
 
1
Photo of niketa yadav
NA 96 958 4y
Hello both , Thankyou for your quick response. I tried with the same URL with server which I am using but again I have stuck in the problem even after talking to service provider ,they said the problem is in config file but I don't know what is it.Please suggest me any other solution if you have .
1
Photo of Jayant Tripathy
678 1.2k 291.5k 4y
Hi niketa, You may pass wrong URL after host in server. As per your API configuration the URL shoud be like
 
http://apiserver:4393/api/controller/GetReaderData
 
OR
 
if the server have no port then simply it may be  
 
http://apiserver/api/controller/GetReaderData.
 
Also at same time check if youa are passing the input parameters or not. 
 
"The resource you are looking for has been removed, had its name changed, or is temporarily unavailable." 
 
As per this error you may ask to service provider once is the application is properly active or not. If you have Cpanel for this restart the IIS once. 
1
Photo of Jay Krishna Reddy
14 53.3k 5.4m 4y
I think you may calling with the wrong URL 
 
Please call with the URL on which you have been hosted and with the appropriate endpoints.
 
Hope this helps you , Thanks