3

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.

4
  • Check out this blog post: strathweb.com/2015/12/running-asp-net-5-website-on-iis Commented Dec 22, 2015 at 18:16
  • Sorry, maybe it wasn't clear, but I did already and have included a link to it at the bottom of my post. Commented Dec 22, 2015 at 18:17
  • 1
    You said localhost:20488/api/Values/1 returns the expected json value, but you never said what gets returned when you access it via IIS? Are you using an IIS Virtual directory? E.g. localhost/MyApp/api/Values/1 Commented Dec 22, 2015 at 19:20
  • Sorry, you are correct, I thought I had included that before. I get an 404 Error. Commented Dec 22, 2015 at 19:36

1 Answer 1

4

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"
      }
    }
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Yep, this quite annoying. I wish there was a way of telling the hosting Url when dealing with IIS so you could call app.Map with it. For my purposes, I have a app that will be deployed to multiple customers... they will choose the IIS virtual Folder... is there a better way of doing this?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.