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