asp.net MVC3 's pseudo-Static implementation code _ practical skills

Source: Internet
Author: User
Tags httpcontext
Now begin to study the first step, how to define your own routing rules to achieve pseudo static functional requirements.

The basic principle of realization of the following diagram:

First, about namespaces.

The function of routing is to make all ASP.net Web site development available, so DLLs are not in MVC, but in system.web System.web.Routing.

Now we implement the custom routing functionality in MVC3 (inheriting routebase, overriding Routedata and Virtualpathdata) for our actual needs.

The following example implements the following: Enter a youdomin.com/product/123.html to perform the TestController index.
The first step: realize the Testroute class
1 routedata Each access URL will be from this portal
Through the HttpContext.Request.AppRelativeCurrentExecutionFilePath to obtain our access to the URL address, based on the address analysis: is not in line with our rules, In line with our rules, we take specific controller and action. The code is as follows:

Copy Code code 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 (URL). 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 requestcontext, routevaluedictionary values)
{
return null;
}
}

In the example above, we do not return NULL if we perform specific controller and specific action, depending on whether a particular value is met in the URL.
The second step is to register our own paths and rules in the global.aspx:
Copy Code code as follows:

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 the red section of the above Code, Application_Start () registers a routing rule, registerroutes (routetable.routes) and then adds the following code to the RegisterRoutes method:
Routes. ADD (New Testroute ("Product"));
Note: Testroute is the route we define above, the class that implements the RouteBase.
Step Three: New controller for testing in the first step
Copy Code code as follows:

public class Testcontroller:controller
{
Public ActionResult Index (string p)
{
viewdata["T"] =p;
Return View ("");
}
}

Fourth Step: Create a new view
Copy Code code as follows:

@{
Layout = null;
}
<! DOCTYPE html>
<title></title>
<body>
<div>
<!--here will display the URL address you entered-->
@ViewData ["T"]. ToString ()
</div>
</body>

Step Fifth, enter the URL test directly
For example: http://127.0.0.1/product/1.html
Follow-up additions:
Main content: How to let the foreground list show page display the pseudo static URL implemented in the 5 steps above?
After practical 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, from the Routedata entry, According to their own routing rules to resolve to the corresponding controller and action, and then in the use of url.action from the Virtualpathdata to resolve the routing rules in accordance with the URL address, the specific code is as follows:
Copy Code code as follows:

public override Virtualpathdata GetVirtualPath (RequestContext requestcontext, routevaluedictionary values)
{
if (values["Controller"]. ToString (). Contains ("Test")
{
Return to New Virtualpathdata (this, "product/" + values["P"] + ". html");
}
Else
return null;
}

You can replace the GetVirtualPath method in the Testroute class in the first step to see 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.