简体   繁体   English

如何启动 webApi Asp.net

[英]How to start a webApi Asp.net

I'm trying to start an ASP.NET Web API which I created but page always appears not found.我正在尝试启动我创建的 ASP.NET Web API,但始终找不到页面。

My controller:我的控制器:

using System;
using System.Collections.Generic;
using System.Linq;
using CrudAPI.ViewModel;
using Microsoft.Extensions.Logging;
using System.Net;
using System.Net.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using Newtonsoft.Json;

namespace CrudAPI.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class ClienteController : ControllerBase
    {
        private readonly ILogger<ClienteController> _logger;

        public ClienteController(ILogger<ClienteController> logger)
        {
            _logger = logger;
        }

        [HttpGet]
        [Route("ListarClientes")]
        public List<Cliente> ListarClientes()
        {
            return new List<Cliente>();
        }
    }
}

I click start from Visual Studio with IIS Express and it open this link:我使用 IIS Express 从 Visual Studio 单击开始,然后打开此链接:

   https://localhost:44395/cliente

I also tried to start a page through port 5000 and 5001 - but I keep getting我还尝试通过端口 5000 和 5001 启动一个页面 - 但我不断收到

404 not found. 404 未找到。

Your url is incorrect, there is no route defined for it.您的 url 不正确,没有为其定义路由。 When you set this: [Route("api/[controller]")], it means that all the routes of the controller will start with "api/cliente".当你设置这个:[Route("api/[controller]")] 时,意味着控制器的所有路由都会以“api/cliente”开头。 So, to access your get route, you have to call: https://localhost:44395/api/cliente/listarclientes因此,要访问您的获取路线,您必须调用:https://localhost:44395/api/cliente/listarclientes

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM