- Hands-On Full:Stack Web Development with ASP.NET Core
- Tamir Dresher Amir Zuker Shay Friedman
- 287字
- 2021-06-10 19:37:27
Routing in SPAs
In SPAs, the complexity needed from ASP.NET Core MVC routing is low, because routing will mostly be taken care of by the frontend code. On the ASP.NET Core MVC end, we will use routing to support client-side routing capabilities.
For this, we will take advantage of conventional routing and set up two catch-all routes, as follows:
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
routes.MapSpaFallbackRoute(
name: "spa-fallback",
defaults: new { controller = "Home", action = "Index" });
});
The first default route was discussed earlier in this section. This will enable all incoming requests to be directed to the Index method of the HomeController.
The second route uses the MapSpaFallbackRoute method, instead of the common MapRoute method. The difference between these methods is a small but important one: the MapSpaFallbackRoute method registers a route just like MapRoute does, but whenever a route matches its template and has a file extension at the end, the URL will be treated as a static file request and will return the file as is, without executing an action method. For example, a call to http://example.com/users will execute the Index method within the UsersController class. A call to http://example.com/images/user.png will return a file named user.png from a folder named images.
This capability is extremely important for SPAs that request many static files from the server, such as images, videos, HTML pages, JavaScript files, CSS files, and more.
These two routes enable everything that is needed for the frontend of the SPA to operate correctly. The first route enables using different MVC controllers, which is sometimes needed in large SPAs, and the second route enables the use of routing on the client-side and the serving of static files when requested.
- EDA技術(shù)與VHDL編程
- 工業(yè)控制網(wǎng)絡(luò)安全技術(shù)與實(shí)踐
- 重新定義Spring Cloud實(shí)戰(zhàn)
- 中小型局域網(wǎng)組建、管理與維護(hù)實(shí)戰(zhàn)
- Metasploit Penetration Testing Cookbook
- 圖解手機(jī)元器件維修技巧
- IPv6網(wǎng)絡(luò)切片:使能千行百業(yè)新體驗(yàn)
- Kong網(wǎng)關(guān):入門(mén)、實(shí)戰(zhàn)與進(jìn)階
- 通信十年:擁抱互聯(lián)網(wǎng)
- VMware NSX網(wǎng)絡(luò)虛擬化入門(mén)
- 互聯(lián)網(wǎng)+思維與創(chuàng)新:通往未來(lái)的+號(hào)
- 網(wǎng)絡(luò)工程實(shí)施技術(shù)與方案大全
- 網(wǎng)絡(luò)利他行為研究:積極心理學(xué)的視角
- 深入理解計(jì)算機(jī)網(wǎng)絡(luò)
- Cisco無(wú)線(xiàn)局域網(wǎng)配置基礎(chǔ)