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 realize the matching operation to URL by ASP.NET MVC

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

Share

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

This article will explain in detail how to match ASP.NET MVC to URL. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

1. Use {parameter} for fuzzy matching

{parameter}: a string of arbitrary length in curly brackets, which cannot be defined as controller and action letters. The default is fuzzy matching.

For example: {admin}.

Using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mvc;using System.Web.Routing;namespace MVCURLMatch {public class RouteConfig {public static void RegisterRoutes (RouteCollection routes) {routes.IgnoreRoute ("{resource} .axd / {* pathInfo}") / 1. Use parameter for fuzzy matching routes.MapRoute (name: "Default", url: "{controller} / {action} / {id}", defaults: new {controller = "Home", action = "Index", id = UrlParameter.Optional});} 2. Use literal values for exact matching

The literal value is a constant number string and cannot have {} outside. This string can be between large parentheses and large parentheses, or at the beginning and end.

For example: admin/ {controller} / {action} / {id}

The URL1:/admin/home/index/1 can match the routes defined above.

URL2:/home/index/1 cannot match the route defined above (missing literal admin)

/ / 2. Use literal quantities to match routes.MapRoute (name: "Default2", url: "admin/ {controller} / {action} / {id}", defaults: new {controller = "Home", action = "Index", id = UrlParameter.Optional})

The result of running when admin is missing in URL:

Correct URL:

Note: admin is also case-insensitive at this time.

3. Continuous URL parameters are not allowed

It is not possible to have no literal value between two parentheses (the two brackets must be followed by a fixed letter or match, otherwise it is impossible to distinguish which parameter).

{language}-{country} / {controller} / {action} / {id} is correct

{language} {country} / {controller} / {action} / {id} error

/ / 3. Continuous URL parameters routes.MapRoute (name: "Default3", url: "{language}-{country} / {controller} / {action} / {id}", defaults: new {controller = "Home", action = "Index", id = UrlParameter.Optional}) are not allowed.

Running result:

You can get the values of the language and country parameters using the URL parameter values described in the previous file, but I'm not going to talk about how to get them here.

4. Use the * sign to match the rest of URL

Use * to match the rest of the URL, for example, * plus is placed at the end of an expression, and the URL part of the end is saved as a dictionary value with the key name plus.

Routes.MapRoute (name: "Default4", url: "{controller} / {action} / {id} / {* plus}", defaults: new {controller = "Home", action = "Index", id = UrlParameter.Optional})

Output the value of the plus parameter in the Index method:

Public ActionResult Index (string plus) {string value = string.Format ("plus= {0}", plus); ViewData ["msg"] = value; return View ();}

Running result:

5. URL greedy matching

There is a special case in URL expressions: there may be multiple matches between URL expressions and the actual URL, which follows the principle of greedy matching.

As you can see from the figure above, the principle of greedy matching is to match URL from back to front.

Routes.MapRoute (name: "Default5", url: "{controller} / {action} / {id} / {filename}", defaults: new {controller = "Home", action = "Index", id = UrlParameter.Optional})

Get the values of filename and ext parameters in the index method, and output them to the page

This is the end of this article on "how to match ASP.NET MVC to URL". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it out 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