首页新闻找找看学习计划

ASP.Net Core WebApi和ASP.Net WebApi有什么不一样嘛?

0
悬赏园豆:5 [已解决问题] 解决于 2018-02-11 08:46

如题:ASP.Net Core WebApi和ASP.Net WebApi有什么不一样嘛?约详细越好,谢谢求,大神指点一下,谢谢了

坚持的孤独的主页 坚持的孤独 | 初学一级 | 园豆:86
提问于:2017-11-07 10:10
< >
分享
最佳答案
1

应该有挺多地方不一样的,具体的话呢,我简单列了一下:

配置项

 以前是写在Global.asax.cs, 的这个方法里面 WebApiConfig.Register

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;

namespace ProductsApp
{
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // Web API configuration and services

            // Web API routes
            config.MapHttpAttributeRoutes();

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
        }
    }
}

现在的话,写在 Startup.cs 里面了

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

namespace ProductsCore
{
    public class Startup
    {
        public Startup(IHostingEnvironment env)
        {
            var builder = new ConfigurationBuilder()
                .SetBasePath(env.ContentRootPath)
                .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
                .AddEnvironmentVariables();
            Configuration = builder.Build();
        }

        public IConfigurationRoot Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            // Add framework services.
            services.AddMvc();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            loggerFactory.AddConsole(Configuration.GetSection("Logging"));
            loggerFactory.AddDebug();

            app.UseMvc();
        }
    }
}

Controller 中的变化

路由配置的变化(在 Startup 中)

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    app.UseMvc(routes =>
    {
        routes.MapWebApiRoute("DefaultApi", "api/{controller}/{id?}");
    });
}

还有,原来的 Controller 是继承至 APIController 的,在. NET-Core 中并不是,
下面是一个.NET-Core 中Controller 的一个例子

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;

namespace ProductsCore.Controllers
{
    [Route("api/[controller]")]
    public class ValuesController : Controller
    {
        // GET api/values
        [HttpGet]
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }

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

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

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

        // DELETE api/values/5
        [HttpDelete("{id}")]
        public void Delete(int id)
        {
        }
    }
}

关于更加详细的不同,可以参考这个文档

收获园豆:5
BUTTERAPPLE | 老鸟四级 |园豆:2948 | 2017-11-07 10:36
其他回答(2)
0

ASP.NET Core Web API 与ASP.NET Web API 主要来自 ASP.NET Core 与 ASP.NET 的不一样

dudu | 园豆:39092 (高人七级) | 2017-11-07 10:39
0

两套不一样的框架,就这样理解。

幻天芒 | 园豆:36522 (高人七级) | 2017-11-07 13:16
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册