Exploring ASP. net mvc-reducing the use of HTML. routelink or URL. routeurl

Source: Internet
Author: User
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.

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.