Webapi Overview
The role of the global file:
| 12345678910111213141516 |
publicclassMvcApplication : System.Web.HttpApplication{ protectedvoidApplication_Start() { //1.注册区域路由 AreaRegistration.RegisterAllAreas(); //2.注册webApi的路由 WebApiConfig.Register(GlobalConfiguration.Configuration); //3.注册全局过滤器 FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); //4.注册网站路由 RouteConfig.RegisterRoutes(RouteTable.Routes); //5.优化js、cs BundleConfig.RegisterBundles(BundleTable.Bundles); }} |
WEBAPI Features
Class must inherit Apicontroller
The return type is no longer actionresult
The default is to request a method with the same name as HttpMethod in the WEBAPI controller
Sets the default return format for the current WEBAPI to json--remove XML format
Config. Formatters.remove (config. Formatters.xmlformatter);
Web site access Webapi, to provide the appropriate URL interface
| 1234567891011121314151617181920212223242526 |
//http://localhost:4221/api/values/getpiglistprotectedvoidPage_Load(objectsender, EventArgs e){ //1.拼装要请求数据的url stringurl = "http://localhost:4221/api/values/getpiglist"; //2.利用C#模拟浏览器发出请求 WebRequest request = WebRequest.Create(url); //2.1设置请求方式,如果不设置则默认为get request.Method = "get"; //3.获取响应报文 WebResponse response = request.GetResponse(); //4.获取相应报文体的字节数据byte[] System.IO.Stream responseBody = response.GetResponseStream(); //5.将字节数据转换为字符串 using(StreamReader sr=newStreamReader(responseBody)) { //5.1将相应报文体转换为json字符串 stringresponseBodyText = sr.ReadToEnd(); //5.2利用json序列化器将json字符串反序列化为list集合 System.Web.Script.Serialization.JavaScriptSerializer jss = newSystem.Web.Script.Serialization.JavaScriptSerializer(); List<Entity> list = jss.Deserialize(responseBodyText, typeof(List<Entity>)) asList<Entity>; GridView1.DataSource = list; GridView1.DataBind(); }} |
Dynamic page Turn static
1 Private voidMakestaticpage (stringURL)2 {3 stringtxthtml ="";4 stringFileName = Guid.NewGuid (). ToString () +". html";5 //1.c# Impersonation browser making a request6WebRequest Request =webrequest.create (URL);7 //2. Get the Response object8WebResponse response =request.getresponse ();9 //3. Get the byte stream based on the response messageTenStream stream =Response. GetResponseStream (); One //4. Byte stream is converted to HTML text and stored in the corresponding text A using(StreamReader sr =NewStreamReader (stream)) - { -txthtml =Sr. ReadToEnd (); theFile.writealltext (Server.MapPath ("/staticpage/"+fileName), txthtml); - } -Response.Write ("<script>alert (' static success! '); </script>"); -}
Third, the MVC plug-in system