ASP.NET Core WebAPI default route not working

守給你的承諾、 提交于 2019-11-29 09:31:00

As @tmg mentioned, do the following:

Right click your web project -> Select Properties -> Select the Debug tab on the left -> Then edit the 'Launch Url' field to set your own default launch url.

You can change the default route by modifying LaunchSettings.json file as shown

Follow the steps below.

Create a base controller for your API that extends base controller of dotnet core:

using Microsoft.AspNetCore.Mvc;

namespace WebApi.Controllers
{
    [Route("api/[controller]")]
    public abstract class ControllerApiBase : Controller
    {

    }
}

And inherit the base class in your API controllers:

using Microsoft.AspNetCore.Mvc;
using WebApi.Dtos;

namespace WebApi.Controllers
{
    public class PingController : ControllerApiBase
    {
        public PingDto Get()
        {
            return new PingDto
            {
                Version = "0.0.0"
            };
        }
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!