14th Chapter Advanced Routing Features

Source: Internet
Author: User
Tags actionlink

----------------------------------------------------------

Principle: 1. the most specific route should be defined first.

2. It is recommended that you provide values for all fragment variables in the URL pattern.

Note: 1. Html.ActionLink when matching, preceded by the interrupt principle (after providing the value, the following variables must provide values, break the default value chain)

@Html. RouteLink method, specifying a route generation link (route name)

2. when parameters are passed to the ActionLink method, be careful to pass the remaining variable values null.

3. in the Web. config below the area, remove the <add namespace= " System.Web.Optimization"/> 

Tip: 1. In order to use the reserved keyword , add the @ symbol in front, such as: New{id= "Myid", @class = "Mycssclass"}

2.html.actionlink used to generate a link to a tag, url.action used to generate a URL file is not a link

* Use URL scheme to generate an output URL that can be embedded in the view so that the user can click on the link,

and to the correct controller and action method as the goal, the form is handed to the application. (True TM nonsense, is to use Html.ActionLink and other methods to generate the correct link.) )

----------------------------------------------------------

Directory:

Generate an output URL in a view

Custom Routing System

Use area

URL Scenario Best Practices

One. Route matching for outbound URLs:

1. Investigate the three conditions for route matching:

(1) Each fragment variable defined in the URL pattern must have a value that is available.

(2) In the value supplied to the fragment variable, there should be no violation of the default variable defined by the route that does not appear

(default-only variables provides a value for which the default value, but not the variable that appears in the URL pattern) . (with default variables)

(3) The values of all fragment variables must satisfy the routing constraints.

Note: The routing system does not attempt to find the best matching route. Only the first matching route is identified, and then the URL is generated using this route.

Any subsequent routes will be ignored. The URL generation process is terminated when the first route object that satisfies the condition produces a non-empty URL.

The parameter values selected for each fragment parameter are replaced in it, and the trailing default sequence of values is ignored.

 If some parameters are explicitly provided, but they do not match the fragment parameter or the default parameter, the method appends the parameters as a query string of the name/value pair.

The routing system is eager to match as much as possible based on a route, and in this sense it reuses the value of the fragment variable that entered the URL.

@Html. ActionLink ("Output url", "Index", "Home", "https", "www.baidu.com", "Myfragementname", new{id= "MyId"},new{id= " Myanchorid ", @class =" Mycssclass "})

Tag Value, Action,contorller, protocol value, target server name, URL fragment, fragment variable, html tag attribute

<a href= "Https://www.baidu.com/Home/Index/MyId#myFragmentName" id= "Myanchorid" class= "Mycssclass" > Output url< /a>

  Generate URL (not link): @Url. Action ("Index", "Home", new {id= "Myid"}) 

String Myactionurl=url.action ("Index", "Home", new {id= "Myid"}) 

Redirecttorouteresult result=redirecttoaction ("Index")

            Redirecttorouteresult result=redirecttoaction (New {controller= "Home", action= "index", id= "MyId"})

Second, custom routing system (can be used when the system is extended)

  1. The custom RouteBase implements two methods :

Getroutedata (HttpContextBase HttpContext): inbound URL matching mechanism . The framework calls this method in turn for each entry in Routetable.routes,

Until one of them returns a non-null value.

Getvirturalpath (RequestContext requestcontext,routevaluedictionary values): outbound URL generation mechanism . The framework sequentially

Each entry of Routetable.routes calls this method until one of them returns a non-null value.

  2. Custom route handlers Implement the Iroutehandler interface:

    Gethttphandler (RequestContext requestcontext) generates the IHttpHandler interface, which is responsible for processing the request.

In the MVC implementation of this interface, the implementation is done: find the Controller, invoke the action method, render the view, and write the result to the response.


Iii. areas of Use

Each region represents a functional segment of the application. Each MVC region has its own folder structure to keep things separate.

This makes the project element more relevant to each of the application's ribbon, and helps multiple developers work on the same project without causing conflicting.

Regional access is widely supported by the system.

Zones are created under the areas folder, with a zone name (such as admin) as a sub-zone folder and include Controller, Models, and Views folders.

The Views folder includes the shared folder and the Web. config file. There is a class named "Adminarearegistration".

The Adminarearegistration class defines the Registerarea method, registering a route with a URL pattern of Admin/{controller}/{action}/{id}.

In the Global.asax call area registration: arearegistration.registerallareas ();


Iv. best method of URL scheme

1.URL Neat and humane

(1) Design URLs to describe their content, not the details of the application. Example:/articles/annualreport, not /website_v2/cachedcontentserver/fromcache/annualreport

   (2) Use the content, not the ID number, whenever possible. Example:/articles/annualreport, not /articles/2392

If you have to use an ID number, add the content. /articles/2392/annualreport

(3) Do not use file extensions for HTML pages, except for special file types (. jpg,.pdf,.zip)

(4) Create a sense of hierarchy. Example:/products/menswear/shirts/red, so you can guess the URL of the parent directory.

(5) Case insensitive. Asp. NET routing system is case insensitive by default.

(6) Avoid using symbols, codes, and character sequences. Word Separation with "-" short horizontal (my-great-article), not recommended underline. Never use a space.

(7) Do not modify the URL. Breaking a link equals losing business. You must continue to support legacy URL schemes for as long as possible with permanent redirection (301) When you have to modify them.

(8) have consistency. A URL format is used throughout the application. URLs should be short, easy to input, editable, and stable to visualize the structure of the site.

2.Get and post Use selection:

    A get request should be used for all read-only information retrieval ( secure interaction ), and thepost request should be used for all actions that modify the state of the program ( change Interaction ).

    

   

    

    

  

14th Chapter Advanced Routing Features

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.