locked
Webapi working locally but not when hosting with sub-domains RRS feed

  • Question

  • User411771660 posted

    I am building a simple webapi which retrieves customers balance from db.Everthing works well locally hosted in IIS.The api takes one param from the body.

     public static void Register(HttpConfiguration config)
        {
            config.Routes.MapHttpRoute("MobileBankingApi", "v1/{controller}/{action}");   
        }


    And the Action

     [HttpPost]
        [ActionName("balance")]
        public IHttpActionResult GetBalance([FromBody]Account account)
        {
            account.balance = BusinessLayer.Api.AccountHolderApi.GetBalance(account.accountNumber);
            return Ok(account);
        }

    Locally works fine with this uri

    http://localhost/v1/accounts/balance

    And when i deploy the build to a live server with subdomain as

    http://api.mydomain.com/v1/accounts/balance

    i get the 404 error.What i am doing wrong?Help appreciated.

    Tuesday, May 30, 2017 10:56 AM

All replies

  • User1967761114 posted

    Hi tarun02,

    In your case, I think there has no affect to access the Web API with domain name.

    According to your description, I’m not sure why the Web API becomes can’t access.

    Could the Web API is working well? try to access the Web API help page, such like the following url.

    http://api.mydomain.com/help

    That was auto-generated by visual studio when create the Web API project, it will displayed all APIs.

    For more details about help page, you could refer to the following link.

    https://docs.microsoft.com/en-us/aspnet/web-api/overview/getting-started-with-aspnet-web-api/creating-api-help-pages

    Could you find the API which you wants in help page?

     

    If you have any other questions, please feel free to contact me any time.

    Best Regards

    Even

    Wednesday, May 31, 2017 5:25 AM
  • User411771660 posted

    Hello sir, i added a new file called index.html in the root and tried to access the file like this http://api.mydomain.com/index.html but i am getting 404 error now.

    Wednesday, May 31, 2017 9:23 AM
  • User1967761114 posted

    Hi tarrun,

    According to your description,it seems like that your site doesn't working,could you sure the configuration of sub-domain was correct?

    If you have any other questions, please feel free to contact me any time.

    Best Regards

    Even

    Thursday, June 1, 2017 7:28 AM
  • User411771660 posted

    Hello.to test i placed a test.html file in the application root.and it shows up in browser.when i go to http://api.mydomain.com/test.html..but my api call

    to http://api.mydomain.com/v1/accounts/balance?accountNumber=CS-0001 gives 404 error

    Thursday, June 1, 2017 11:11 AM
  • User-1946294156 posted

    If you haven't such it down, typically (by default) ASP.NET Web API has a help page to display the link of the Web API.  It is an MVC Page that comes with the project (again, by default if you used the project template for Web API and not an empty solution/project).  It is typically at http://yourdomain.com/help.

    Thursday, June 1, 2017 12:34 PM
  • User1879451342 posted

    Might it help to add route in controller?

    [System.Web.Http.Route("api/app/v1/post")]
    [HttpPost]
     public IHttpActionResult Post(DTO request)
    { }



    Thursday, June 1, 2017 8:16 PM
  • User411771660 posted

    Hello sir, i used an empty solution/project.Not getting to the to the test.html from the root of the application is resolved had some issues with my DNS.but the problem is the api works very well on my development server with http://localhost/v1/accounts/balance but when i deploy the same build to my live server(windows server 2008r2) and use localhost in binding and not the subdomain it still gives me 404 error.However in my live server http://localhost/index.html gives me the response.

    Thursday, June 1, 2017 8:38 PM
  • User411771660 posted

    Hello sir, i used an empty solution/project.Not getting to the to the test.html from the root of the application is resolved had some issues with my DNS.but the problem is the api works very well on my development server with http://localhost/v1/accounts/balance but when i deploy the same build to my live server(windows server 2008r2) and use localhost in binding and not the subdomain it still gives me 404 error.However in my live server http://localhost/index.html gives me the response.

    Thursday, June 1, 2017 8:39 PM
  • User1967761114 posted

    Hi tarun02,

    Could you try to access http://api.mydomain.com/help as my first reply? Does that works?

    If not, might has some errors on IIS,I suggest try to install .Net Framework for IIS on the server by the following command.

    Framework 4.0,4.5 :

    C:\Windows\Microsoft.NET\Framework\v4.0.30319>aspnet_regiis.exe -i

    Framework 3.5,2.0 :

    C:\Windows\Microsoft.NET\Framework\v2.0.50727>aspnet_regiis.exe -i

    And after above if still return 404,try to modify the configuration file such like the following configuration.

    <system.webServer>
        <modules runAllManagedModulesForAllRequests="true" />
        ...
    </system.webServer>

    If you have any other questions, please feel free to contact me any time.

    Best Regards

    Even

    Friday, June 2, 2017 8:54 AM
  • User1879451342 posted

    This is a direction I would head in:

    Could you try to access http://api.mydomain.com/help as my first reply? Does that works?

    The help page will display what url to add to the domain.   Ie:  GET /api/Roles

    The key is to track down what url is being called vs what should be called.  I usually have a few tools I use for this:

    1. I usually have some type of logging that logs what url is being called, errors, etc, to a text file on the server.  
    private void WriteToFile(string text)
    {
                
        string path = "C:\\wwwroot\\site\\Test.txt";
        using (StreamWriter writer = new StreamWriter(path, true))
        {
            writer.WriteLine(string.Format(text + DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss tt")));
            writer.Close();
        }
    }
    1. I also use something like Fiddler or Wireshark to trace the call.
    2. Also, In most my APIs, I have Swashbuckle installed while developing.  When I deploy, I enter the domain, then /swagger, ie, http://localhost:55660/swagger .   This gives me a little more info than the help page.  
    3. Log onto server and get the raw IP address or use System.Net library:
    static void Main(string[] args)
    {
    string hostName = Dns.GetHostName(); // Retrive the Name of HOST
    Console.WriteLine(hostName);
    
    // Get the IP
    IPAddress[] localIps = Dns.GetHostAddresses(Dns.GetHostName());
    foreach (var x in localIps)
    {
        Console.WriteLine("My IP Address is :" + x);
    }
                
    Console.ReadKey();
    }



    Friday, June 2, 2017 1:56 PM