Asp.net MVC learning diary 5 (custom routehandler)

Source: Internet
Author: User

1. Add in global. asax

Routes. maproute ("dbcontent", "{* path}", new {controller = "home"}). routehandler = new catalogroutehandler ();

2. Add the catalogroutehandler class in the Controller folder to inherit from iroutehandler.

Public class catalogroutehandler: iroutehandler
{
Public ihttphandler gethttphandler (requestcontext)
{
String Path = requestcontext. routedata. Values ["path"] as string;

If (Path = NULL)
{
Requestcontext. routedata. Values ["action"] = "Index ";
// Add the path minus the command back in
// Requestcontext. routedata. Values ["path"] = path;
}
Else
{

// Remove trailing slash if there is one
If (path. endswith ("/"))
Path = path. substring (0, path. Length-1 );
If (path! = NULL & path. Contains ("/") // valid path Parameter
{
Int lastindex = path. lastindexof ('/');
If (lastindex> = 0)
{
String commandname = path. substring (lastindex + 1 );

Switch (commandname. toupper ())
{
Case "get": // get the catalog item
Requestcontext. routedata. Values ["action"] = "get ";
// Add the path minus the command back in
Requestcontext. routedata. Values ["path"] = path. substring (0, lastindex );
Break;
Case "delete": // Delete catalog item
Requestcontext. routedata. Values ["action"] = "delete ";
// Add the path minus the command back in
Requestcontext. routedata. Values ["path"] = path. substring (0, lastindex );
Break;
Case "edit": // edit catalog item
Requestcontext. routedata. Values ["action"] = "edit ";
// Add the path minus the command back in
Requestcontext. routedata. Values ["path"] = path. substring (0, lastindex );
Break;
Case "Post": // save catalog item (insert/update)
Requestcontext. routedata. Values ["action"] = "Post ";
// Add the path minus the command back in
Requestcontext. routedata. Values ["path"] = path. substring (0, lastindex );
Break;
Default: // we will allow nothing to act as a get
Requestcontext. routedata. Values ["action"] = "get ";
// Add the path minus the command back in
Requestcontext. routedata. Values ["path"] = path;
Break;
}

}
}
}

Return new mvchandler (requestcontext );
}
}

 

3. Add get, delete, edit, and post actions to homehandler. CS respectively. And add the corresponding View

Public actionresult get (string path)
{
Return view ();
}
Public actionresult Delete (string path)
{
Return view ();
}
Public actionresult post (string path)
{
Return view ();
}
Public actionresult edit (string path)
{
Return view ();
}

Add home, such as http: // loaclhost: 1124/home/edit

OK.

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.