1

I am perplexed as to why my Web API straight out of the box is not working locally. I have created a new Web Api in Visual Studio 2017 .NET core, here is the controller:

namespace WebAPICore.Controllers
{
    [Route("api/[controller]")]
    public class ValuesController : Controller
    {
        // GET api/values
        [HttpGet]
        public IEnumerable<string> Get()
        {
            return new string[] { "Hello", "Hello 2" };
        }

        // GET api/values/5
        [HttpGet("{id}")]
        public string Get(int id)
        {
            return "value";
        }

        // POST api/values
        [HttpPost]
        public void Post([FromBody]string value)
        {
        }

        // PUT api/values/5
        [HttpPut("{id}")]
        public void Put(int id, [FromBody]string value)
        {
        }

        // DELETE api/values/5
        [HttpDelete("{id}")]
        public void Delete(int id)
        {
        }
    }
}

I am running via IIS as webapicore.local/ so I am trying webapicore.local/api/values in the browser and I am getting a 404! It is also not working with Postman:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>IIS 10.0 Detailed Error - 404.0 - Not Found</title>
        <style type="text/css">
            <!-- 
body{margin:0;font-size:.7em;font-family:Verdana,Arial,Helvetica,sans-serif;} 
code{margin:0;color:#006600;font-size:1.1em;font-weight:bold;} 
.config_source code{font-size:.8em;color:#000000;} 
pre{margin:0;font-size:1.4em;word-wrap:break-word;} 
ul,ol{margin:10px 0 10px 5px;} 
ul.first,ol.first{margin-top:5px;} 
fieldset{padding:0 15px 10px 15px;word-break:break-all;} 
.summary-container fieldset{padding-bottom:5px;margin-top:4px;} 
legend.no-expand-all{padding:2px 15px 4px 10px;margin:0 0 0 -12px;} 
legend{color:#333333;;margin:4px 0 8px -12px;_margin-top:0px; 
font-weight:bold;font-size:1em;} 
..............

Any ideas?

1
  • If you are running in IIS then there might be an application name. So it could be webapicore.local/application/api/values Commented Aug 5, 2019 at 16:50

2 Answers 2

1

I have never seen webapicore.local/, but if that is not necessary for your project, look in the autogenerated Properties/launchSettings.json file. Mine looks like this after creating a new web application project:

{
  "$schema": "http://json.schemastore.org/launchsettings.json",
  "iisSettings": {
    "windowsAuthentication": false, 
    "anonymousAuthentication": true, 
    "iisExpress": {
      "applicationUrl": "http://localhost:59313",
      "sslPort": 0
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "launchUrl": "api/values",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "WebApplication1": {
      "commandName": "Project",
      "launchBrowser": true,
      "launchUrl": "api/values",
      "applicationUrl": "https://localhost:5001;http://localhost:5000",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

After launching with IIS navigate to https://localhost:59313/api/values, and it should work right out of the box.

Again though, if webapicore.local/ is necessary for your project this may not be your solution.

Sign up to request clarification or add additional context in comments.

Comments

0

Try the same route but using the default port number 5000: webapicore.local:5000/api/values. This is assuming you haven't overridden the default application url in launchSettings.json.

1 Comment

Thank you for your answers, I feel a bit of an idiot, as I have I have just spoke to someone and they said you can't run a .NET Core web API directly from IIS, you need to publish the app, then install ASP.NET Core hosting bundle for Windows, then point IIS to the publish folder. Now everything works as expected! See more here: medium.com/tengweisong/…

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.