简体   繁体   中英

how to develop simple web api using ASP .net core webapi?

I'm new to .net core, I'm trying to build a calculator web API. I struggling to find the exact way to do this. I managed to build API controller part with multiple get methods which return math operation. Below is the code

//MathController.cs file.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace Calculationwebapi.Controllers
{
    public class MathController : ApiController
    {
        [HttpGet]
        public int Add(int value1, int value2)
        {
            return value1 + value2;
        }

        [HttpGet]
        public int Substract(int value1, int value2)
        {
            return value1 - value2;
        }

        [HttpGet]
        public int Multiply(int value1, int value2)
        {
            return value1 * value2;
        }

        [HttpGet]
        public int Divide(int value1, int value2)
        {
            return value1 / value2;
        }

        [HttpGet]
        public string Get()
        {
            return "default";
        }
    }
}

What must be the project structure and how should I proceed next? Do I need to build model and view file as in .net core MVC? i built this in .net web API just by creating a web API server with API controller and configuring web api I used json to consume web API server. In .net core both MVC and web API are together so I'm little bit confused doing in .net core

您可以使用dotnet命令(从命令行)轻松创建Web api项目, dotnet new webapi将创建项目,请参见https://docs.microsoft.com/zh-cn/dotnet/core/tools/dotnet-new?tabs= netcore21

要在 .net core 中创建 web api,你必须有一个 db 类,你已经在其中编写了所有的 linq 操作来获取数据,然后右键单击 sollution 并添加新项目,然后转到 web 并选择 ASP .net web 应用程序和之后选择一个空模板,不要忘记检查 web api,然后你就可以开始编写 web api了。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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