官术网_书友最值得收藏!

Getting started

In the old days of web applications, things were simple—if you wanted a page, you had to have a physical one. However, things have since evolved and ASP.NET Core is now an MVC framework. What does that mean? Well, in MVC, there are no such thing as physical pages (although this is not exactly true);instead, it uses routing to directrequeststo routehandlers. The most common routehandlers in MVC arecontroller actions. After this chapter, you will learn how to use routing to access your controller actions.

A request is just some relative URL, such as this:

/Search/Mastering%ASP.NET%Core
/Admin/Books
/Book/1

This results in more readable URLs, and is also advantageous for search engines such as Google. The subject of optimizing a site—including its public URLs—for search engines is called Search Engine Optimization (SEO).

When ASP.NET Core receives a request, one of the following two things can happen:

  • There is a physical file that matches the request.
  • There is a route that accepts the request.

In order for ASP.NET Core to serve physical files, it needs to be configured—for that, we use the UseStaticFiles extension method in Configure, which adds the static files, processing middleware to the pipeline; the call to UseStaticFiles is included in the Visual Studio template for ASP.NET Core web applications. If we don't enable static file serving, or if no file exists, the requests need to be handled by a route handler. The most common route handler in MVC is a controller action.

A controller is a class that exposes an action that knows how to process a request. An action is a method that may take parameters and returns an action result. A routing table is what we use to direct requests to controlleractions.

There are two APIs that we can use to register routes:

  • Fluent API (code)
  • Attributes

In previous versions, we had to explicitly add support for routing attributes, but they are now first-class citizens of .NET Core. Let's go through them, starting with the routing table concept.

主站蜘蛛池模板: 黔西县| 平湖市| 永安市| 礼泉县| 新邵县| 永修县| 防城港市| 旬阳县| 神木县| 县级市| 沐川县| 曲阜市| 巨野县| 托里县| 石渠县| 奉化市| 前郭尔| 海口市| 玛多县| 房产| 壤塘县| 宣化县| 兴宁市| 贡嘎县| 鄂尔多斯市| 怀仁县| 大竹县| 邹平县| 桃源县| 弥勒县| 昌江| 大庆市| 靖江市| 滁州市| 北安市| 和龙市| 稷山县| 称多县| 安龙县| 肇庆市| 江西省|