Web api hosting

bigFan 21 Reputation points
2021-06-30T22:44:09.007+00:00

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

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,390 questions
ASP.NET API
ASP.NET API
ASP.NET: A set of technologies in the .NET Framework for building web applications and XML web services.API: A software intermediary that allows two applications to interact with each other.
306 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Sam of Simple Samples 5,516 Reputation points
    2021-06-30T23:01:06.77+00:00

    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.