peinearydevelopment
peinearydevelopment

Reputation: 11474

Running ASP.NET 5 Web.API on IIS

I created an application in ASP.NET 5. For the moment I'm just trying to expose a very simple Web.API method to try to get this to work. The Controller looks like this:

using System.Threading.Tasks;
using Microsoft.AspNet.Mvc;

namespace Api
{
    [Route("api/[controller]")]
    public class ValuesController : Controller
    {
        [HttpGet("{id}")]
        public async Task<Value> Get(int id)
        {
            return await Task.Factory.StartNew(() => new Value { Id = id });
        }
    }
}

I tried to follow both tutorials provided in the asp.net documentation here and here.

I have published my website to a location on my machine and have setup IIS to point to it. I have enabled the IIS logging. When I try to browse from IIS and go to the url http://localhost/TestApi/api/Values/1, I get a 404 response in the browser. It is interesting to me as well because in the ResponseHeaders I have one that looks like this: Server: Kestrel. Shouldn't this be IIS?

I also get a log file in my logs directory and it looks like this:

Hosting environment: Production
Now listening on:http://localhost:20488
Application started. Press Ctrl+C to shut down.

When I go to http://localhost:20488/api/Values/1 though, I get my JSON object returned. { Id: 1 }

I can't figure out what I'm doing wrong. I've searched a lot to try to find out what the proper configuration is to get this to work with IIS and still haven't found the answer.

Any idea what I'm doing wrong or how I can get this to work as expected?

I have reviewed these blog posts as well which seem to be great resources, but don't have any information on the problem I'm having: strathweb, blogs.msdn

UPDATE: I tried this on Windows 10 with IIS 10 and am experiencing the same issue, so I removed the IIS 7.5 from the Title of the question as it doesn't seem to be IIS 7.5 specific.

Upvotes: 3

Views: 3115

Answers (1)

peinearydevelopment
peinearydevelopment

Reputation: 11474

After much searching I finally found how to solve my problem. I didn't post my Startup.cs file before because I didn't think that it would be relevant to the question as the application seemed to be working correctly outside of IIS. I still believe that is true, but there seems to be a known issue with the new ASP.NET 5 apps working in IIS. (NOTE: While that issue is closed it references other issues which are not. One of the comments in one of the linked issues seems to say this will be fixed with RC2.) Before my Configure method looked like this in Startup.cs

public void Configure(IApplicationBuilder app)
{
    app.UseIISPlatformHandler();
    app.UseMvc();
}

I needed to change that method to look like this:

public void Configure(IApplicationBuilder app)
{
    app.UseIISPlatformHandler();
    app.Map("/TestApi", builder => builder.UseMvc());
}

Apparently, IIS just passes the URL it gets straight to the application and since the URL was coming in as http://localhost/TestApi/api/Values/1, the application didn't know how to handle the route with "TestApi" in it. Once I changed that and Published it again. The application responded as expected when going to that URL through IIS.

To make my life easier during development so that I could run the application through VS and IIS without having to change the second line of the Configure method, there were two options. One would have been to wrap the Map call in an if statement and only call the Map function when in Production otherwise call app.UseMvc(). What I chose to do though was I changed my launchSettings.json I updated it to look like this:

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:49482/TestApi",
      "sslPort": 0
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "Hosting:Environment": "Development"
      }
    },
    "web": {
      "commandName": "web",
      "environmentVariables": {
        "Hosting:Environment": "Development"
      }
    }
  }
}

Upvotes: 4

Related Questions