config.Routes.MapHttpRoute("img", "img/{name}", New With {.controller = "Img", .action = "g"}) config.Routes.MapHttpRoute("css", "css/{name}", New With {.controller = "Css", .action = "g"}) config.Routes.MapHttpRoute("js", "js/{name}", New With {.controller = "Js", .action = "g"}) config.Routes.MapHttpRoute("API", "{controller}/{action}/{id}", New With {.id = RouteParameter.Optional}) ' vs2010 just can not IntelliSense.... config.Routes.MapHttpRoute("default", "", New With {.controller = "Home", .action = "g"})
接下來則是加入 ImgController, CssController, JsController, HomeController 這四個 class
Public Class ImgController Inherits ApiController Dim accept_type As Dictionary(Of String, String) = New Dictionary(Of String, String) From {{".jpg", "jpg"}, {".png", "png"}, {".gif", "gif"}} <HttpGet()> Public Function g(ByVal name As String) As HttpResponseMessage Dim content As New StreamContent(New System.IO.FileStream(Request.RequestUri.LocalPath.Substring(1), IO.FileMode.Open)) Dim ext As String = System.IO.Path.GetExtension(Request.RequestUri.LocalPath).ToLower() If accept_type.ContainsKey(ext) Then content.Headers.ContentType = New MediaTypeHeaderValue("image/" & ext) End If Dim response = New HttpResponseMessage() With {.Content = content} 'response.Headers.CacheControl = New CacheControlHeaderValue() With {.MaxAge = New TimeSpan(1, 0, 0)} Return response End Function End Class
Public Class CssController Inherits ApiController <HttpGet()> Public Function g(ByVal name As String) As HttpResponseMessage Dim content As New StreamContent(New System.IO.FileStream(Request.RequestUri.LocalPath.Substring(1), IO.FileMode.Open)) content.Headers.ContentType = New MediaTypeHeaderValue("text/css") Dim response = New HttpResponseMessage() With {.Content = content} 'response.Headers.CacheControl = New CacheControlHeaderValue() With {.MaxAge = New TimeSpan(1, 0, 0)} Return response End Function End Class
Public Class JsController Inherits ApiController <HttpGet()> Public Function g(ByVal name As String) As HttpResponseMessage Dim content As New StreamContent(New System.IO.FileStream(Request.RequestUri.LocalPath.Substring(1), IO.FileMode.Open)) content.Headers.ContentType = New MediaTypeHeaderValue("text/javascript") Dim response = New HttpResponseMessage() With {.Content = content} 'response.Headers.CacheControl = New CacheControlHeaderValue() With {.MaxAge = New TimeSpan(1, 0, 0)} Return response End Function End Class
Public Class HomeController Inherits ApiController <HttpGet()> Public Function g() As HttpResponseMessage
Dim content As New StreamContent(New System.IO.FileStream("html/first.html", IO.FileMode.Open)) content.Headers.ContentType = New MediaTypeHeaderValue("text/html") Dim response As New HttpResponseMessage() With {.Content = content} Return response End Function End Class
Private Sub WebBrowser1_DocumentTitleChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles WebBrowser1.DocumentTitleChanged Me.Text = WebBrowser1.DocumentTitle End Sub
Imports System.Web.Http Public Class BlahController Inherits ApiController <HttpGet()> Public Function getDate() As String Return System.DateTime.Today.ToString("yyyy/MM/dd") End Function End Class
完成了程式碼的安排後,還要記得黑大提醒的一件事,在 Windows 7、Windows 2008 這些作業系統開發時,因為權限控管較嚴,開 port 監聽需要較高權限來開放。我選擇了用 netsh 這個方法來開放權限。先開啟命令列(記得要用 administrator 權限開啟命令列)
大名鼎鼎的 NuGet,是 Microsoft 開發平台(包含 .NET)的 package manager。因為 Microsoft 開發有太多方便的套件可用,以往都是開發者自行在網路上下載、安裝,難免會有安裝維護上的困擾。而現在透 NuGet,不但方便一般開發者,有統一的搜尋、下載、安裝介面,也方便套件開發者的散佈、維護。