In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how to build a simple Web Api in ASP.NET Core MVC. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
Getting Started
In the ASP.NET Core MVC framework, the ASP.NET team provides us with a complete set of suites needed to build various parts of a Web, so what if we just need to do a simple Web Api program?
In the ASP.NET Core MVC source code in GitHub, we only need to focus on the Microsoft.AspNetCore.Mvc package, and it contains these in addition to this package:
Microsoft.AspNetCore.Mvc.ApiExplorer
Microsoft.AspNetCore.Mvc.Cors
Microsoft.AspNetCore.Mvc.DataAnnotations
Microsoft.AspNetCore.Mvc.Formatters.Json
Microsoft.AspNetCore.Mvc.Localization
Microsoft.AspNetCore.Mvc.Razor
Microsoft.AspNetCore.Mvc.TagHelpers
Microsoft.AspNetCore.Mvc.ViewFeatures
Microsoft.Extensions.Caching.Memory
Microsoft.Extensions.DependencyInjection
NETStandard.Library
Typically, when we create a Web MVC site, we add the following in the ConfigureServices method in the Startup.cs file:
Services.AddMvc ()
The above code injects the services in MVC into the DI container. Let's take a look at the source code of AddMvc ():
Public static IMvcBuilder AddMvc (this IServiceCollection services) {var builder = services.AddMvcCore (); builder.AddApiExplorer (); builder.AddAuthorization (); AddDefaultFrameworkParts (builder.PartManager); / / Order added affects options setup order / / Default framework order builder.AddFormatterMappings (); builder.AddViews (); builder.AddRazorViewEngine (); builder.AddCacheTagHelper (); / / + 1 order builder.AddDataAnnotations (); / / + 1 order / / + 10 order builder.AddJsonFormatters () Builder.AddCors (); return new MvcBuilder (builder.Services, builder.PartManager);} simple Web Api
In fact, if you want to build a simple Web Api program, the ASP.NET team has thought of this for us, so we only need to modify the services we injected.
First, instead of referencing the package Microsoft.AspNetCore.Mvc, refer to Microsoft.AspNetCore.Mvc.Core. Mvc.Core this package will only provide you with basic MVC middleware, such as routing, Controller, HttpResult, etc., other more such as Razor,Cores,Views is not provided.
In Web Api applications, in most cases, data serialization is transmitted by Json, so you need to add the Microsoft.AspNetCore.Mvc.Formatters.Json package.
Then, in ConfigureServices, add Mvc Core middleware and Json Formatter.
Public void ConfigureServices (IServiceCollection services) {services.AddMvcCore () .AddJsonFormations ();}
Finally, your XXXController class should inherit ControllerBase instead of Controller. There is no support for Views in ControllerBase.
Public class XXXController: ControllerBase {}
Here are all the packages referenced by the final project.json.
"dependencies": {"Microsoft.NETCore.App": {"version": "1.1.0", "type": "platform"}, "Microsoft.AspNetCore.Mvc.Core": "1.1.0", "Microsoft.AspNetCore.Mvc.Formatters.Json": "1.1.0", "Microsoft.AspNetCore.Server.IISIntegration": "1.1.0" "Microsoft.AspNetCore.Server.Kestrel": "1.1.0", "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.1.0", "Microsoft.Extensions.Configuration.FileExtensions": "1.1.0", "Microsoft.Extensions.Configuration.Json": "1.1.0", "Microsoft.Extensions.Configuration.CommandLine": "1.1.0", "Microsoft.Extensions.Logging": "1.1.0" "Microsoft.Extensions.Logging.Console": "1.1.0", "Microsoft.Extensions.Logging.Debug": "1.1.0"} this article on "how to build a simple Web Api in ASP.NET Core MVC" ends here. Hope that the above content can be helpful to you, so that you can learn more knowledge, if you think the article is good, please share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.