Search code examples
asp.netasp.net-web-apiasp.net-coreiis-expresskestrel-http-server

set base path in IISExpress and webapi in ASP.NET 5


I need to have WebAPI project working under different base path than usual. I created simple project under Visual Studio that uses WebAPI and ASP.NET 5.

Under base path set to http://localhost:38170/ my project works fine and I'm able to get values from test controller (http://localhost:38170/api/values). At this stage my IIS Express configuration is:

<site name="WebApi" id="2">
    <application path="/" applicationPool="Clr4IntegratedAppPool">
        <virtualDirectory path="/" physicalPath="C:\WebApi\src\WebApi\wwwroot" />
   </application>
   <bindings>
        <binding protocol="http" bindingInformation="*:38170:localhost" />
   </bindings>
</site>

I tried changing App URL under project properties to reflect my need: http://localhost:38170/xxx

Now running project and hitting http://localhost:38170/xxx/api/values results in 404. Trying http://localhost:38170/api/values returns values from controller just as if nothing changed. I noticed that changes in Visual Studio are not reflected in IIS Express configuration (I don't know if they should be...) in any way.

I tried changing path on IISExpress manually like in this thread: Creating virtual directories in IIS express.

<site name="WebApi" id="2">
    <application path="/" applicationPool="Clr4IntegratedAppPool">
        <virtualDirectory path="/xxx" physicalPath="C:\WebApi\src\WebApi\wwwroot" />
   </application>
   <bindings>
        <binding protocol="http" bindingInformation="*:38170:localhost" />
   </bindings>
</site>

The results are:

http://localhost:38170/api/values - 500.19 Error (config error) and that is fairly ok - I don't plan this to work

http://localhost:38170/xxx/api/values - 502.3 - Bad Gateway on hitting httpPlatformHandler

I suppose that error is somewhere in httpPlatformHandler configuration but I'm not sure how to do it in conjuction with IIS Express. My web.config is:

<configuration>
  <system.webServer>
    <handlers>
      <add name="httpPlatformHandler" path="*" verb="*" modules="httpPlatformHandler" resourceType="Unspecified"/>
    </handlers>
    <httpPlatform processPath="%DNX_PATH%" arguments="%DNX_ARGS%" stdoutLogEnabled="false" startupTimeLimit="3600"/>
  </system.webServer>
</configuration>

I tried random changes like changes in path attribute to xxx/* but nothing works.

EDIT: To clarify the question.

How to setup WebAPI on ASP.NET 5 (ASP.NET Core) on IISExpress using httpPlatformHandler and Kestrel to set base path other than root.


Solution

  • You corrupt the file as your modification does my honor the IIS Express configuration rules.

    I will suggest you use a smart tool such as Jexus Manager to manipulate it, and then you can sync the Visual Studio project with the correct URL.

    For example, the 404 is expected, as your application tag has path set to /, so there is no application nor virtual directory to serve xxx.

    The 500.19 later is also expected, as while adding a valid virtual directory named xxx, you deleted the root virtual directory. That's totally wrong as a root virtual directory must present.