1、Global.asax中添加
routes.MapRoute("DbContent", "{*path}",new { controller = "Home" }).RouteHandler = new CatalogRouteHandler();
2、在Controller文件夹下,添加CatalogRouteHandler类,让其继承自IRouteHandler
public class CatalogRouteHandler : IRouteHandler
{
public IHttpHandler GetHttpHandler(RequestContext 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、在HomeHandler.cs分别添加Get,Delete,Edit,Post的Action。并添加其对应视图
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();
}
访问的时候加上Home,如http://loaclhost:1124/Home/Edit
ok.