Using WebApi on WinHost

Discussion in 'Site Programming, Development and Design' started by daveharney, Jan 21, 2014.

  1. I've put together a very basic tutorial for a jQuery Mobile app with a very basic tutorial using the Web Api with VS 2013 and VB.NET. The Web Api is just for use with my UI within my app as a way to bridge between html and asp.net. It works perfectly on my development machine but the Web Api part fails when I move it to Winhost. Do I need to change one of the config files or does Winhost have to do something to allow Web Api?

    Here is the html/JS/jQuery code that calls the Asp.net code:
    HTML:
     var uri = 'api/class1s';  // sets up the URI
            function find() {
                var id = $('#prodId').val();
                $.getJSON(uri + '/' + id)              //  calls the URI with a search id provided below
                    .done(function (data) {
                        $('#product').text(formatItem(data));
                    })
                    .fail(function (jqXHR, textStatus, err) {
                        $('#product').text('Error: ' + err);
                    });
            }
    There is an input field to provide the id used in the $.getJSON above
    
                <div>
                    <input type="text" id="prodId" size="5" />
                    <input type="button" value="Search" onclick="find();" />
                    <p id="product" />
                </div>
    The web.config is:
    Code:
           <configuration>
          <system.web>
        <compilation debug="true" targetFramework="4.5.1" />
        <httpRuntime targetFramework="4.5.1" />
         </system.web>
         <system.webServer>
        <handlers>
          <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
          <remove name="OPTIONSVerbHandler" />
          <remove name="TRACEVerbHandler" />
          <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler"       preCondition="integratedMode,runtimeVersionv4.0" />
        </handlers>
         </system.webServer>
        </configuration>
    There is also a WebApiConfig:
    Code:
         Imports System
        Imports System.Collections.Generic
        Imports System.Linq
        Imports System.Web.Http
    
        Public Module WebApiConfig
        Public Sub Register(ByVal config As HttpConfiguration)
            ' Web API configuration and services
    
            ' Web API routes
            config.MapHttpAttributeRoutes()
    
            config.Routes.MapHttpRoute(
                name:="DefaultApi",
                routeTemplate:="api/{controller}/{id}",
                defaults:=New With {.id = RouteParameter.Optional}
            )
         End Sub
        End Module
    
    There is also a class to define the data and a "controller" to populate data and return data via the Web Api. These should not affect this problem (and work perfectly on my desktop PC). Otherwise, this is just a standard VS2013 ASP.Net 4.5.1 project using an empty website with the Web Api option selected. Is there a config change needed in my code or does Winhost have to do something to enable Web Api?
     
    Last edited by a moderator: Oct 14, 2015
  2. I should have known better! I was so focused on this having only html web pages that I forgot that the VB code behind was still ASP.NET. I forgot to configure the folder for asp. It works fine now.
     
    ComputerMan likes this.
  3. ComputerMan

    ComputerMan Winhost Staff

    Glad you resolved your problem :)
     

Share This Page