question

bigFan-1849 avatar image
0 Votes"
bigFan-1849 asked Bruce-SqlWork commented

Web api hosting

Hello everyone, I am new to web api. I have this simple question about web api host. So here is how a client application consume the web api:

 public class StudentController : Controller
 {
     // GET: Student
     public ActionResult Index()
     {
         IEnumerable<StudentViewModel> students = null;
    
         using (var client = new HttpClient())
         {
             client.BaseAddress = new Uri("http://localhost:64189/api/");
             //HTTP GET
             var responseTask = client.GetAsync("student");
             responseTask.Wait();
    
             var result = responseTask.Result;
             if (result.IsSuccessStatusCode)
             {
                 var readTask = result.Content.ReadAsAsync<IList<StudentViewModel>>();
                 readTask.Wait();
    
                 students = readTask.Result;
             }
             else //web api sent error response 
             {
                 //log response status here..
    
                 students = Enumerable.Empty<StudentViewModel>();
    
                 ModelState.AddModelError(string.Empty, "Server error. Please contact administrator.");
             }
         }
         return View(students);
     }
 }

So when you run the client application locally, it's fine. But now when you publish your client application to the hosting provider and accessing the site through domain for example www.example.com, what's going to happen now? Will your application still be able to access the web api service since there is no localhost anymore on the hosting service? I read on this link https://www.tutorialsteacher.com/webapi/web-api-hosting.

It seems to say that if the web api is part of the mvc project, then it should work, will it?

Also one last question about self hosting, how is it step up if you use a hosting provider for your domain and site and don't have your own server for hosting?

Thanks

dotnet-csharpdotnet-aspnet-webapi
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

SimpleSamples avatar image
0 Votes"
SimpleSamples answered Bruce-SqlWork commented

localhost is nothing except a different name for the IP address 127.0.0.1. localhost is not software. I assume that your system is executing IIS; that is the server in your system. When you pay a hosting provider they are using a server such as IIS or Apache and their computers are always on. You can configure a domain's DNS to know to use the hosting provider's version of your website. If you are using the application in a website configured in your local IIS as a website then it should work the same in an IIS implementation in a hosting provider.

Correction: Visual Studio provides a developer version of a web server and uses it for running websites developed using VS. For the purpose of understanding hosting providers, the VS server is the same as an IIS server in an IIS hosting provider.

Have you used Azure? You can create an account for free. You can create a website for free that does not use a custom domain (such as www.example.com) but that you can see how your application works in an IIS hosting provider. Then if you want to use a custom domain for it then that will cost a little bit but the application will work the same; you just need to configure the DNS to know to go to the website when someone browses to the domain.






· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank you. Yes localhost is just a different name for the ip address. So look at this code:

 client.BaseAddress = new Uri("http://localhost:64189/api/");

Will this still work when you publish it on the hosting provider since the url is localhost? Or another way to put it, will the web api service still accessible through the url above?

0 Votes 0 ·

Just want to add a bit more on my previous comment. See if the url is something like a relative path (~/api/) instead of a fixed path like "http://localhost:64189/api/". So when you publish it to www.example.com. Then the path will be www.example.com/api/, But it is fixed path. Will my file on www.example.com be able to access the service on http://localhost:64189/api/?

0 Votes 0 ·

As I said, http://localhost:64189/api/ will be seen as http://127.0.0.1:64189/api/ and the 64189 is there I believe because that is why the server that VS uses works. Anyway see the accepted answer in how do i get around baseaddress. Since that is an ASP.Net question the best place to get answers is in the ASP.Net forum. If you search elsewhere for asp.net BaseAddress Web api then you will find many other suggestions.




0 Votes 0 ·

localhost means the current server. so if you deploy to a hosting server, it will call the api on that hosting server at port 64189. is that what you meant?

if you meant it to call your local box, that will not work. you would need a public DNS for your box, and use that address.

0 Votes 0 ·