WEBAPI Overview of Personal understanding

Source: Internet
Author: User

Webapi Overview
  1. 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);    }}
  2. WEBAPI Features

      1. Class must inherit Apicontroller

      2. The return type is no longer actionresult

      3. The default is to request a method with the same name as HttpMethod in the WEBAPI controller

  3. Sets the default return format for the current WEBAPI to json--remove XML format

      1. Config. Formatters.remove (config. Formatters.xmlformatter);

  4. 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();    }}
  5. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.