简体   繁体   English

找不到api / values 404本地主机-ASP.NET Core

[英]api/values 404 not found localhost - ASP.NET Core

I am perplexed as to why my Web API straight out of the box is not working locally. 对于为什么我的Web API不能直接在本地工作,我感到困惑。 I have created a new Web Api in Visual Studio 2017 .NET core, here is the controller: 我在Visual Studio 2017 .NET核心中创建了一个新的Web Api,这是控制器:

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 ! 我通过IIS以webapicore.local/身份运行,因此我在浏览器中尝试使用webapicore.local/api/values ,并且得到404 It is also not working with Postman: 它也不适用于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? 有任何想法吗?

I have never seen webapicore.local/ , but if that is not necessary for your project, look in the autogenerated Properties/launchSettings.json file. 我从未见过webapicore.local/ ,但是如果您的项目不需要这样做,请查看自动生成的Properties / launchSettings.json文件。 Mine looks like this after creating a new web application project: 在创建新的Web应用程序项目后,我的情况如下所示:

{
  "$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. 使用IIS启动后,导航至https:// localhost:59313 / api / values ,它应立即可用。

Again though, if webapicore.local/ is necessary for your project this may not be your solution. 再说一次,如果您的项目需要webapicore.local /,那么这可能不是您的解决方案。

Try the same route but using the default port number 5000: webapicore.local:5000/api/values . 尝试相同的路由,但使用默认端口号5000: webapicore.local:5000/api/values This is assuming you haven't overridden the default application url in launchSettings.json . 这是假设您尚未覆盖launchSettings.json的默认应用程序URL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 覆盖 404 在 Asp.Net 核心 Web API 中找不到 - Override 404 Not found in Asp.Net core Web API 未找到返回 404 的 Asp.Net Core“Web API”路由 - Asp.Net Core "Web API" Routes Returning 404 Not Found ASP.Net Core“找不到此本地主机页面”HTTP 错误 404 - ASP.Net Core "This localhost page can’t be found" HTTP ERROR 404 localhost在已有的web api添加razor页面后返回404(ASP.net 6核心) - localhost returns 404 after adding razor page to the existing web api (ASP.net 6 core) ASP.NET Core API Swagger - No webpage was found for the web address: https://localhost:44358 - ASP.NET Core API Swagger - No webpage was found for the web address: https://localhost:44358 尝试从 ASP.NET Core 5 Web 应用程序运行 Api 控制器功能时找不到 404 - 404 not found when trying to run Api Controller function from ASP.NET core 5 web app ASP.NET CORE Web API - 删除未触发的操作(未找到404) - ASP.NET CORE Web API - delete action not firing (404 not found) 将 asp.net core web api 部署到 aws elastic beanstalk 时出现错误 404 Not Found - Getting Error 404 Not Found when deployed asp.net core web api to aws elastic beanstalk ASP.NET Core MVC:找不到这个本地主机页面 - ASP.NET Core MVC : this localhost page can’t be found ASP.NET 核心“找不到这个本地主机页面” - ASP.NET Core “This localhost page can’t be found”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM