Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of MVC Route customization and View finding rules

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail the example analysis of MVC routing customization and view finding rules. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.

MVC routing customization

I believe we all know something about the configuration of MVC routing. In fact, this is not the focus of this chapter.

When you create a MVC project, go to the root directory > > App_Start > > RouteConfig. This class is used to configure routing, and there will be a default route at the beginning.

Briefly introduce: * parameters: route name (*), the second parameter url: note that {controller} and {action} are equivalent placeholders. The third parameter is the default route.

Scenario: when your controller needs to be classified according to different functions, if you need to make different products, you want to put the controllers of the same product together. You can do this:

Add a routing rule:

Public static void RegisterRoutes (RouteCollection routes) {routes.IgnoreRoute ("{resource} .axd / {* pathInfo}") Routes.MapRoute (name: "Product", url: "Product/AboutPrice/ {controller} / {action} / {id}", defaults: new {controller = "Sale", action = "Index", id = UrlParameter.Optional}) Routes.MapRoute (name: "Default", url: "{controller} / {action} / {id}", defaults: new {controller = "Home", action = "Index", id = UrlParameter.Optional});}

Note: routing rule matching is from top to bottom, and priority matching routes must be written at the top. Because after the route match is successful, it will not continue to match. Of course, the controller directory we created has to match to match.

Results:

MVC view to find rules

In the above section, even if we customize the routing, the location of the view creation still has to be placed under the Views folder. (otherwise, the view will not be found.) We also want the View location to be configurable.

You want the view returned by the controller to be in this directory:

① first, we need to write a class to customize the mapping rules for our view. Here I build it under the App_Start folder, along with other configuration classes.

Public sealed class MyViewRule:RazorViewEngine {public MyViewRule () {ViewLocationFormats = new string [] {/ / the view lookup rules we defined match from top to bottom, just like the routing configuration. If there are views under both paths, it will display *. "~ / ProductViews/AboutPrice/ {1} / {0} .cshtml", / / this is set by default and can be deleted if it is not needed. {1} represents the controller name, and {0} represents the view name "~ / Views/ {1} / {0} .cshtml", "~ / Views/Shared/ {0} .cshtml"} }}

② then, we need to register our rules in the global Global.asax.cs, as follows:

Public class MvcApplication: System.Web.HttpApplication {protected void Application_Start () {AreaRegistration.RegisterAllAreas (); FilterConfig.RegisterGlobalFilters (GlobalFilters.Filters); RouteConfig.RegisterRoutes (RouteTable.Routes); BundleConfig.RegisterBundles (BundleTable.Bundles) / / above is the default registration for project creation. The following two items: first clear the original rules, and then add () ViewEngines.Engines.Clear (); ViewEngines.Engines.Add (new MyViewRule ());}}

Test results:

Error: The view at'/ ProductViews/AboutPrice/Sale/Index.cshtml' must derive from WebViewPage, or WebViewPage.

This WebViewPage class is not inherited. At compile time, the view page will also generate a class. There are two ways to solve this problem:

① lets it inherit that class directly (@ inherits System.Web.Mvc.WebViewPage)

@ {Layout = null;} @ inherits System.Web.Mvc.WebViewPage Index my view position is different!

Of course, the downside of the above method is that you have to write it every time you add a view, which is extremely inconvenient if there are too many views. We can also make it inherit from WebViewPage through the configuration file.

② adds a configuration file (web.config) that inherits WebViewPage under the pages node. Or a Web.config is copied to / ProductViews/AboutPrice under the default creation Views folder. (partial profile, proximity principle)

Results:

This is the end of the article on "sample Analysis of MVC routing customization and View search rules". I hope 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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report