Global文件的作用:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
public class MvcApplication : System.Web.HttpApplication { protected void Application_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特点
类必须继承ApiController
返回类型不再是ActionResult
默认是请求WebApi控制器中的和HttpMethod同名的方法
设置当前WebApi的默认返回格式为json--移除xml格式
config.Formatters.Remove(config.Formatters.XmlFormatter);
网站访问WebApi,要提供相应的url接口
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
//http://localhost:4221/api/values/getpiglist protected void Page_Load( object sender, EventArgs e) { //1.拼装要请求数据的url string url = "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= new StreamReader(responseBody)) { //5.1将相应报文体转换为json字符串 string responseBodyText = sr.ReadToEnd(); //5.2利用json序列化器将json字符串反序列化为list集合 System.Web.Script.Serialization.JavaScriptSerializer jss = new System.Web.Script.Serialization.JavaScriptSerializer(); List<Entity> list = jss.Deserialize(responseBodyText, typeof (List<Entity>)) as List<Entity>; GridView1.DataSource = list; GridView1.DataBind(); } }
|
动态页面转静态
1 private void MakeStaticPage(string url) 2 { 3 string txtHtml = ""; 4 string fileName = Guid.NewGuid().ToString() + ".html"; 5 //1.C#模拟浏览器发出请求 6 WebRequest Request = WebRequest.Create(url); 7 //2.获取Response对象 8 WebResponse response = Request.GetResponse(); 9 //3.根据响应报文获取字节流 10 Stream stream = response.GetResponseStream(); 11 //4.字节流转换为html文本,并存储到相应文本中 12 using (StreamReader sr = new StreamReader(stream)) 13 { 14 txtHtml = sr.ReadToEnd(); 15 File.WriteAllText(Server.MapPath("/staticPage/" + fileName), txtHtml); 16 } 17 Response.Write("<script>alert(‘静态化成功 !‘);</script>"); 18 }
三、MVC插件系统
原文:http://www.cnblogs.com/notniu/p/3898001.html