加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net – Visual Studio中Web API和Azure API APP之间的区别

发布时间:2020-12-16 06:55:34 所属栏目:asp.Net 来源:网络整理
导读:在创建新的ASP.NET Web应用程序以开发我的API并在Azure中托管它时,我有两个选择: Web API Azure API APP 我可以创建一个Web API APP并将其托管在Azure API APP中吗?那么它们存在的主要原因是什么? 解决方法 根据您的需要,它们只是不同的起点. Azure API是
在创建新的ASP.NET Web应用程序以开发我的API并在Azure中托管它时,我有两个选择:

> Web API
> Azure API APP

我可以创建一个Web API APP并将其托管在Azure API APP中吗?那么它们存在的主要原因是什么?

enter image description here

解决方法

根据您的需要,它们只是不同的起点.

Azure API是一个精简的API模板,支持OpenAPI.

ASP.NET Web API是一个完整的ASP.NET MVC应用程序,主要用于支持API.

差异#1:Web API模板中的身份验证支持

ASP.NET Web API

支持身份验证选择(无,个人用户,工作或学校,Windows).

Screenshot showing selection of authentication support

ASP.NET Azure API应用程序

期望客户提供代币(持票人代币或API代币).使用Azure门户配置用户(不是API令牌)的身份验证和授权.使用Azure API Management(或其他服务)来管理API令牌.

Screenshot showing unavailable selector for authentication support

差异#2:Web API模板中的MVC支持

ASP.NET Web API

自动包含MVC以显示帮助页面.这些与OpenAPI(Swagger)自生成文档不同.

Screenshot showing MVC option preselected,non-editable

ASP.NET Azure API应用程序

不会自动包含MVC或帮助页面

Screenshot showing MVC option not selected,editable

差异#3:支持Web API中的UI文件

ASP.NET Web API

包括区域,内容,HomeController,字体,脚本和视图

Screenshot showing Solution Explorer with expanded assets

ASP.NET Azure API应用程序

Screenshot showing Solution Explorer with minimal assets

差异#4:Web API模板中的更多启动配置

ASP.NET Web API

public class WebApiApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        GlobalConfiguration.Configure(WebApiConfig.Register);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
    }
}

ASP.NET Azure API应用程序

public class WebApiApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        GlobalConfiguration.Configure(WebApiConfig.Register);
    }
}

差异#5:Azure API模板中的OpenAPI(Swagger)支持

ASP.NET Web API

public class ValuesController : ApiController
{
    // GET api/values
    public IEnumerable<string> Get()
    {
        return new string[] { "value1","value2" };
    }

    // GET api/values/5
    public string Get(int id)
    {
        return "value";
    }

    // POST api/values
    public void Post([FromBody]string value)
    {
    }

    // PUT api/values/5
    public void Put(int id,[FromBody]string value)
    {
    }

    // DELETE api/values/5
    public void Delete(int id)
    {
    }
}

ASP.NET Azure API应用程序

默认情况下启用OpenAPI(Swagger). OpenAPI JSON文档位于/ swagger / docs / v1

public class ValuesController : ApiController
{
    // GET api/values
    [SwaggerOperation("GetAll")]
    public IEnumerable<string> Get()
    {
        return new string[] { "value1","value2" };
    }

    // GET api/values/5
    [SwaggerOperation("GetById")]
    [SwaggerResponse(HttpStatusCode.OK)]
    [SwaggerResponse(HttpStatusCode.NotFound)]
    public string Get(int id)
    {
        return "value";
    }

    // POST api/values
    [SwaggerOperation("Create")]
    [SwaggerResponse(HttpStatusCode.Created)]
    public void Post([FromBody]string value)
    {
    }

    // PUT api/values/5
    [SwaggerOperation("Update")]
    [SwaggerResponse(HttpStatusCode.OK)]
    [SwaggerResponse(HttpStatusCode.NotFound)]
    public void Put(int id,[FromBody]string value)
    {
    }

    // DELETE api/values/5
    [SwaggerOperation("Delete")]
    [SwaggerResponse(HttpStatusCode.OK)]
    [SwaggerResponse(HttpStatusCode.NotFound)]
    public void Delete(int id)
    {
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读