簡單說就是要把副檔名拿掉,而這些API很單純,全都是POST,廢話不多說,看code吧。
(下面以VB.NET為範例)
1、在 Global.asax 修改下列程式:
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs) ' 在應用程式啟動時引發 RegisterRoutes(RouteTable.Routes) End Sub Shared Sub RegisterRoutes(ByVal routes As RouteCollection) routes.Add("Item", New Route("{model}", New CompactRESTRouteHandler())) routes.Add("Directory", New Route("{directory}/{model}" , New CompactRESTRouteHandler())) End Sub2、建立一個檔案:CompactRESTRouteHandler.vb
Friend Class CompactRESTRouteHandler Implements IRouteHandler Public Function GetHttpHandler(requestContext As RequestContext) As IHttpHandler Implements IRouteHandler.GetHttpHandler Dim RouteData = requestContext.RouteData '取出參數 Dim directory As String = Convert.ToString(RouteData.Values("directory")) Dim model As String = Convert.ToString(RouteData.Values("model")) Dim id As String = Convert.ToString(RouteData.Values("id")) If String.IsNullOrEmpty(directory) = False Then HttpContext.Current.Items.Add("directory", directory) End If HttpContext.Current.Items.Add("model", model) If String.IsNullOrEmpty(id) = False Then HttpContext.Current.Items.Add("id", id) End If '檢查看看有無該Model對應的ASHX? Dim ashxName As String = Path.Combine(directory, model + ".ashx") '找不到對應的ASHX 'If File.Exists(HostingEnvironment.MapPath("~/" + ashxName)) = False Then ' Return CType(BuildManager.CreateInstanceFromVirtualPath("~/NotFound.aspx", GetType(Page)), Page) 'End If '導向指定的ASHX Return CType(BuildManager.CreateInstanceFromVirtualPath("~/" + ashxName, GetType(IHttpHandler)), IHttpHandler) End Function End Class參考文章: 逐步解說:在 Web Form 應用程式中使用 ASP.NET 路由逐步解說:在 Web Form 應用程式中使用 ASP.NET 路由 RESTful探索3-ASP.NET 3.5 Routing for REST
沒有留言:
張貼留言