Asp.net MVC學習日記五(自訂RouteHandler)

來源:互聯網
上載者:User

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.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.