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

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


富国沪深
浏览 2155回答 1
1回答

翻翻过去那场雪

应该有挺多地方不一样的,具体的话呢,我简单列了一下:配置项 以前是写在Global.asax.cs, 的这个方法里面 WebApiConfig.Registerusing 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)        {         }     } }
打开App,查看更多内容
随时随地看视频慕课网APP