• .Net WEBAPI创建


    1. 在mvc的解决方案中,增加webapi接口。

    新添加Controller,模板选用webapi,由于原来mvc版本使用4.5,所以webapi也使用4.5,创建完成后,会在app_start中创建webapiconfig的配置文件。

     ps:自动创建的无action,需要添加action。

    添加完后,在global.asax文件application_strat中添加

    GlobalConfiguration.Configure(WebApiConfig.Register);(此必须在最前面)

    在Controllers中

    [RoutePrefix("api/WebApi")]
    public class WebApiController : ApiController
    { 

    [HttpGet]
    [Route("Get)]
    public string Get()

    {return "hello";}

          [Route("api/WebApi/Getid/{id}")]
            [HttpGet()] 
           
            public string Getid(int id)
            {
                return "hello world, "+id;
            }

    }

    运行即可,访问localhost:xxxx/api/webapi/get     localhost:xxxx/api/webapi/Getid/2

    另一种:带参数的api访问

    [RoutePrefix("api/WebApi")]
    public class WebApiController : ApiController
    {
    [HttpGet]
    [Route("Getid")]
    public string Getid(int id){xxxxx}

    }

    访问此地址:http://xxxx/api/webapi/Getid?id=2

    2.net core 使用vscode创建webapi

    创建一个文件夹,vscode中打开文件夹,终端dotnet new webapi命令即可创建出api模板。

    运行http://localhost:5000/api/values 

  • 相关阅读:
    webpack性能优化-PWA(离线访问技术)
    node增删改查
    webpack性能优化
    webpack常见配置
    第一个node接口
    $refs的解释
    vue路由传参的三种基本方式
    结构体的定义
    gcc 的编译流程 和gdb的调试方法
    makefile高级用法
  • 原文地址:https://www.cnblogs.com/yokiblogs/p/13266258.html
Copyright © 2020-2023  润新知