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

How to build a more readable ASP.NET Core route

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the knowledge of "how to build a more readable ASP.NET Core route". In the operation of practical cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Step by Step

When building the route for the project, whether it is an uppercase URL route or a lowercase URL route, the first thing we need to ensure is that we need to unify the URL format of the entire project. You can't say that one part of a project has an uppercase URL address while the other part uses a lowercase URL address. At the same time, the uppercase path and lowercase path of the same page, although the final server may point two addresses to the same page, but for the collection of search engines, these are undoubtedly two pages.

Imagine the following: when someone tells us an interesting website, we enter the URL from the address bar of the browser to visit it. When we enter the URL address, whether it is the Chinese input method or the English input method, the output English letters are lowercase, at this time, if there are uppercase letters in the input URL, well, we also need to use the CapsLock key to switch case.

In addition, we know that for the Windows server, because it is not sensitive to the case of the path, we can still have normal access if we get the wrong address case, but if we deploy the application to the Linux server.

As for better readability, the concept may seem subjective. To put it simply, when we are faced with a URL, we can clearly know the main content of the page through this URL. For example, when we see the www.youdomain.com/editor/post/new URL, although we may not open the page, but we can roughly guess that the page may be a new article. However, if you receive a URL like www.youdomain.com/9rg7f2/i?HXI-D+iaj34, no one knows what the page is for.

Therefore, in order to facilitate typing, we first need to convert our URL address to lowercase. In ASP.NET Core, Microsoft provides RoutingServiceCollectionExtensions.AddRouting, an extension that allows us to convert URL address to lowercase.

Open the project's Startup.cs file, find the ConfigureServices method, and add the following code to the method body.

one

two

three

four

Services.AddRouting (options = >

{

Options.LowercaseUrls = true

});

The top link code of the sample project is shown below. Running the project can see that after setting lowercase routing, the URL addresses automatically generated by the program based on Controller and Action are all lowercase. If you look closely, you can see that there will be a problem here. In some special cases, Area/Controller/Action may be a mixed English word made up of multiple English letters, and if all the mixed words are lowercase without splitting, the URL readability of the whole project is even lower.

one

two

three

four

five

six

seven

eight

nine

ten

eleven

twelve

thirteen

fourteen

fifteen

sixteen

seventeen

eighteen

nineteen

twenty

twenty-one

twenty-two

twenty-three

twenty-four

Sample

Home

Privacy

Draft Setting

In the Configure method in the Startup class, we define two routing templates with and without Area. The URL of the entire project is generated based on these two templates. So can we specify a special URL format for a single Controller or Action here?

one

two

three

four

five

six

seven

eight

nine

ten

App.UseMvc (routes = >

{

Routes.MapRoute (

Name: "default"

Template: "{controller=Home} / {action=Index} / {id?}")

Routes.MapRoute (

Name: "areas"

Template: "{area:exists} / {controller=Home} / {action=Index} / {id?}")

});

The answer is of course yes. In ASP.NET Core, we can add user-defined routing information to the routing table of the project by adding RouteAttribute to the Controller or Action method. For example, here I use feature routing on the DraftSetting Action to manually specify the current Action to generate a special URL format.

one

two

three

four

five

six

seven

eight

Public class PostController: Controller

{

[Route ("post/draft-setting")]

Public IActionResult DraftSetting ()

{

Return View ()

}

}

Although this will solve our problem, it seems troublesome to manually specify the feature routing address as soon as the project has a new page. So, how to automatically let the program help us achieve this function?

In ASP.NET Core 2.2, Microsoft provides us with the concept of parameter converter. By implementing the interface IOutboundParameterTransformer, we can convert the values of routing in URL or routing parameters in URL according to our requirements. As in the following code, I implement this interface to separate the mixed words generated by multiple English words in the form of hyphen (-).

one

two

three

four

five

six

seven

eight

nine

Public class SlugifyParameterTransformer: IOutboundParameterTransformer

{

Public string TransformOutbound (object value)

{

Return value = = null

? Null

: Regex.Replace (value.ToString (), "([Amurz]) ([Amurz])", "$1murf 2") .ToLower ()

}

}

Here I use hyphen (-) as the hyphen between words in URL, because for search engines, it regards-as a separator between words, and URL in this style is more conducive to search engines.

When the interface function is implemented, we need to modify our default global route. First, we need to specify the routing parameters to be replaced on the routing template, where we specify that Area, Controller, and Action are the variables that need to be converted.

one

two

three

four

five

six

seven

eight

nine

ten

eleven

App.UseMvc (routes = >

{

Routes.MapRoute (

Name: "default"

Template: "{controller:slugify=Home} / {action:slugify=Index} / {id?}")

Routes.MapRoute (

Name: "areas"

Template: "{area:exists:slugify} / {controller:slugify=Home} / {action:slugify=Index} / {id?}"

);

});

After defining the parameter converter and the URL routing parameters that need to be translated, we can configure the parameter routing values that need to be translated through ConstraintMap in the AddRouting method. At this point, we can complete the results of our routing parameter conversion.

one

two

three

four

Services.AddRouting (options = > {

Options.ConstraintMap ["slugify"] = typeof (SlugifyParameterTransformer)

Options.LowercaseUrls = true

});

That's all for "how to build a more readable ASP.NET Core route". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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

Servers

Wechat

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

12
Report