So sánh các phiên bản netcore

Hãy nâng cấp lên Microsoft Edge để tận dụng các tính năng mới nhất, bản cập nhật bảo mật và hỗ trợ kỹ thuật.

Compare controllers in ASP.NET MVC and Web API with controllers in ASP.NET Core

  • Bài viết
  • 04/14/2022

Trong bài viết này

Tip

This content is an excerpt from the eBook, Porting existing ASP.NET Apps to .NET 6, available on .NET Docs or as a free downloadable PDF that can be read offline.

So sánh các phiên bản netcore

In ASP.NET MVC 5 and Web API 2, there were two different Controller base types. MVC controllers inherited from Controller; Web API controllers inherited from ApiController. In ASP.NET Core, this object hierarchy has been merged. It's recommended that API controllers in ASP.NET Core inherit from ControllerBase and add the [ApiController] attribute. Standard view-based MVC controllers should inherit from Controller.

In both frameworks, controllers are used to organize sets of action methods. Filters and routes can be applied on a controller level in addition to at the action level. These conventions can be extended further by using custom base Controller types with default behavior and attributes applied to them.

In ASP.NET MVC, content negotiation isn't supported. ASP.NET Web API 2 does support content negotiation, as does ASP.NET Core. Using content negotiation, the format of the content returned to a request can be determined by headers the client provides indicating its preferred manner of receiving the content.

When migrating ASP.NET Web API controllers to ASP.NET Core, a few components need to be changed if they exist. These include references to the ApiController base class, the System.Web.Http namespace, and the IHttpActionResult interface. Refer to the documentation for recommendations on how to migrate these specific differences. Note that the preferred return type for API actions in ASP.NET Core is `Controller`0.

In addition, the `Controller`1 attribute isn't supported in ASP.NET Core. In ASP.NET Core, similar functionality is achieved using View Components.

ASP.NET Core includes two new attributes: ConsumesAttribute and ProducesAttribute. These are used to specify the type an action consumes or produces, which can be helpful for routing and documenting the API using tools like Swagger/OpenAPI.

Hãy nâng cấp lên Microsoft Edge để tận dụng các tính năng mới nhất, bản cập nhật bảo mật và hỗ trợ kỹ thuật.

Compare ASP.NET Web API 2 and ASP.NET Core

  • Bài viết
  • 02/16/2023

Trong bài viết này

Tip

This content is an excerpt from the eBook, Porting existing ASP.NET Apps to .NET 6, available on .NET Docs or as a free downloadable PDF that can be read offline.

So sánh các phiên bản netcore

ASP.NET Core offers iterative improvements to ASP.NET Web API 2, but should feel familiar to developers who have used Web API 2. ASP.NET Web API 2 was developed and shipped alongside ASP.NET MVC. This meant the two approaches had similar-but-different approaches to things like attribute routing and dependency injection. In ASP.NET Core, there's no longer any distinction between MVC and Web APIs. There's only ASP.NET Core, which includes support for view-based scenarios, API endpoints, Razor Pages, health checks, SignalR, and more.

In addition to being consistent and unified within ASP.NET Core, APIs built in .NET Core are much easier to test than those built on ASP.NET Web API 2. We'll cover testing differences in more detail in a moment. The built-in support for hosting ASP.NET Core apps, in a test host that can create an HttpClient that makes in-memory requests to the app, is a huge benefit when it comes to automated testing.

See Incremental ASP.NET to ASP.NET Core migration for an incremental approach to migrating to ASP.NET Core.

Download SDK 6.0 : https://dotnet.microsoft.com/en-us/download/dotnet/6.0 (SDK - Software development kit: bộ công cụ phát triển)

  • Download Visual Studio 2022 community (free) https://visualstudio.microsoft.com/downloads/
  • Hoặc sử dụng visual studio code (link hướng dẫn): https://www.c-sharpcorner.com/article/how-to-setup-visual-studio-code-for-c-sharp-10-and-net-6-0/

5. Tạo và chạy ứng dụng đầu tiên với .NET 6

So sánh các phiên bản netcore

6. Các thành phần trong ứng dựng web với .NET 6

So sánh các phiên bản netcore

  • Ví dụ với launchSettings.json ta có thể config aplicationurl hoặc thêm các profiles theo từng môi trường (development, staging, production)
    So sánh các phiên bản netcore
  • Program.cs file chính chạy chương trình khi run ứng dụng. Chứa khai báo các service, config, middleware...

So sánh các phiên bản netcore

(app.run chạy app)

Note: Ở phần này chỉ là tổng quan nên sẽ không giải thích chi tiết về biến môi trường, middleware, razor page hay các khái niệm chi tiết khác...

7. Razor pages

  • Razor là một thành phần trong .NET nó giúp cho phép ta xây dựng giao diện dễ dàng bằng cách sử dụng code c# trong file HTML.
  • Các file Razor có chứa đuôi .cshtml (gồm code c# và html)
  • Razor engine sẽ giúp biến các file razor thành file html và trình duyệt có thể hiểu được.
  • Để sử dụng razor page ta thêm cấu hình
    So sánh các phiên bản netcore

Tham khảo cách sử dụng razor page: https://learn.microsoft.com/vi-vn/aspnet/core/tutorials/razor-pages/model?view=aspnetcore-6.0&tabs=visual-studio