This article has no technical content and is suitable for the homepage of the blog garden under the current situation.
First, define a routing rule:
String [] Controllernamespaces = New String [] { " Web. Controllers " };
Routes. maproute (
" Details " ,
" Details/{productid} " ,
New {Controller = " Product " , Action = " Details " },
New {Productid = @" \ D + " },
Controllernamespaces
);
Previously generated links do this:
Foreach (VAR item In Model ){
Writer. Write (html. routelink (item. title, " Details " , New {Productid = Item. productid }));
}
Now it is changed to the following: String Detailslinkformat = Html. routelink ( " {1} " , " Details " , New {Productid = 0 })
. Tostring (). Replace ( " 0 " , " {0} " );
Stringbulder sb = New Stringbuilder (model. Count ());
Foreach (VAR item In Model ){
SB. append ( String . Format (detailslinkformat, item. productid, item. Title ));
}
Writer. Write (sb. tostring ());
First, generate a link address:
<A href = "/details/0"> {1} </a>
After "0" is replaced with "{0}", it becomes like this:
<A href = "/details/{0}"> {1} </a>
Then we can generate a new link address based on this format.
For the sake of conciseness, the aboveCodeNo empty model verification, URL encoding, HTML encoding, and list formatting are performed.
In addition, Lao Zhao used to analyze the URL generation performance.