Pseudo-static Implementation of ASP. NET mvc3

Source: Internet
Author: User

Recently, I used Asp.net mvc3 to develop B2C e-commerce systems. To optimize Seo, I need to implement pseudo-static URLs through routing, and then generate real static pages as needed, do not walk directly to access the specific page. Now let's take a look at the first step, how to define your own routing rules to meet the pseudo-static functional requirements.

The basic implementation principles are as follows:

 

First, about the namespace.

The routing function is used for all Asp.net website development. Therefore, DLL is not in MVC, but in system. Web, system. Web. routing.

Now we implement custom routing in mvc3 for our actual needs (inherit routebase and rewrite routedata and virtualpathdata ).

In the following example, enter youdomin.com/product/123.htmland execute index in testcontroller.

Step 1: implement the testroute class

1 routedata this portal is used for each URL access

You can use httpcontext. Request. apprelativecurrentexecutionfilepath to obtain the URL address of our access and analyze it based on the address: whether it meets our rules. If it meets our rules, we can use specific controller and action. The Code is as follows:

Public class testroute: routebase
{Private string [] URLs;
Public testroute (Params string [] targeturls ){
URLs = targeturls;
} Public override routedata getroutedata (httpcontextbase httpcontext)
{
Routedata result = NULL;
String requestedurl =
Httpcontext. Request. apprelativecurrentexecutionfilepath + httpcontext. Request. pathinfo;
Requestedurl = requestedurl. substring (2). Trim ('/');

If (requestedurl. Contains (URLs. toarray (). getvalue (0). tostring ()))
{
Result = new routedata (this, new mvcroutehandler ());
Result. Values. Add ("controller", "test ");
Result. Values. Add ("action", "Index ");
Result. Values. Add ("P", requestedurl );
}
Return result;
} Public override virtualpathdata getvirtualpath (requestcontext, routevaluedictionary values)
{
Return NULL;
}

}

In the above example, we determine whether the URL matches a specific value to execute a specific controller and a specific action. If not, null is returned.

Step 2: register our own paths and rules in global. aspx:

 

Public static void registerroutes (routecollection routes)
{
Routes. ignoreroute ("{resource}. axd/{* pathinfo }");

Routes. Add (New testroute ("product "));
Routes. maproute (
"Default", // route name
"{Controller}/{action}/{ID}", // URL with Parameters
New {controller = "home", Action = "Index", id = urlparameter. optional} // parameter defaults
);

}

Protected void application_start ()
{
Arearegistration. registerallareas ();

Registerglobalfilters (globalfilters. filters );
Registerroutes (routetable. routes );
}

 

Note that in the red part of the above Code, register a routing rule in application_start (), and then add the following code to the registerroutes method:

Routes. Add (New testroute ("product "));

Note: testroute is the router class defined above to implement routebase.

Step 3: Create a controller for trial use in step 1

 

Public class testcontroller: Controller
{
Public actionresult index (string P)
{
Viewdata ["T"] = P;
Return view ("");
}
}

Step 4: Create a view

 

@{
Layout = NULL;
}

<! Doctype HTML>

<HTML>
<Head>
<Title> </title>
</Head>
<Body>
<Div>
<! -- The URL you entered is displayed here -->
@ Viewdata ["T"]. tostring ()
</Div>
</Body>
</Html>

 

Step 5: directly enter the URL to test

For example, http: // 127.0.0.1/product/1.html

Subsequent supplements:

Main content:How to display the pseudo static URL in the preceding five steps on the front-end list display page?

After actual verification, it is found that the implementation of virtualpathdata in routebase can solve the above problems .. Net route has actually implemented this two-way parsing problem. By entering the URL, entering from routedata, parsing the corresponding controller and action according to your own routing rules, and then using the URL. the URL address that complies with the routing rules is parsed from virtualpathdata. The specific code is as follows:

public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values)        {            if (values["controller"].ToString().Contains("Test"))            {                return new VirtualPathData(this, "product/" + values["p"] + ".html");            }            else            return null;        }

You can replace the getvirtualpath method in the testroute class in step 1 to view the actual effect.

Related Article

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.