当前位置:首页 > windos > 正文

.Net WEBAPI创建

在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";}

}

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