Asp.net-知识总结(7)

1.回顾:上篇学习和了解了asp.net mvc 的相关知识和EF ,linq


2.jquery(ajax),MVC(ajax),Routing(路由),过滤器(filter)

  

(1)  jquery(ajax)——简单post提交

$(function () {
       $("#btnGetDate").click(function () {
            $.ajax({
                url:"/Student/GetDateNow",
                type: "POST",
                data: "",
                success: function (date) {
                    alert(date);
                    $("#hehh").val(date);
                }
            });
        });
    });


(2)  mvc——ajax

1)  引入

2)注意引用的顺序

l  Ajax.BeginForm没有提供闭合的方法,请使用Using配合关闭

l  AjaxOptions参数的设置

l  SuccessMthod只是提供提醒,并没有数据的传递

 3)代码示例

  

  

 <% using(Ajax.BeginForm("GetDateNow", new AjaxOptions { HttpMethod ="Post", InsertionMode =InsertionMode.Replace,OnSuccess="ShowYesz",UpdateTargetId="resultDiv"}))
           {%>
           dd
<%} %>

 

(3)  mvc分页代码示例

(4)  Routing(路由)

1)介绍

 

2)  MapRoute方法

以下方法的重载

MapRoute( string name, string url);
MapRoute( string name, string url,object defaults);
MapRoute( string name, string url,string[] namespaces);
MapRoute( string name, string url,object defaults, object constraints); MapRoute( string name, string url, objectdefaults, string[] namespaces);
MapRoute( string name, string url,object defaults, object constraints, string[] namespaces);


 

       参数:

name:规则名称, 可以随意起名.不可以重名,否则会发生错误: 路由集合中已经存在名为“Default”的路由。路由名必须是唯一的。

     url:

url获取数据的规则, 这里不是正则表达式, 将要识别的参数括起来即可, 比如: {controller}/{action}

最少只需要传递name和url参数就可以建立一条Routing(路由)规则.比如实例中的规则完全可以改为:

routes.MapRoute( "Default","{controller}/{action}");


 

constraints: 通过使用正则表达式可以规定参数格式,比如controller参数只能为4位数字:new { controller = @"\d{4}"}

string [] : 来指明控制器controller的命名空间

 

代码示例:

l #region 酒店频道部分 //hotels/list-beijing-100,200-3

lroutes.MapRoute( "酒店列表页","hotels/{action}-{city}-{price}-{star}", new { controller ="Hotel", action = "list", city = "beijing",price="-1,-1", star="-1" }, new {city=@"[a-zA-Z]*",price=@"(\d)+\,(\d)+",star="[-1-5]"} ); //hotels/所有匹配

lroutes.MapRoute( "酒店首页", "hotels/{*values}", new {controller = "Hotel", action = "default", hotelid ="" } );

l#endregion //网站首页.

     routes.MapRoute( "网站首页", "{*values}", new { controller = "Home",action = "index"} ); }


 

(5).过滤器

类实现:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
 
namespace MVCShow.Models
{
    [AttributeUsage(AttributeTargets.All,AllowMultiple=true)]
   public class DemoFilterAttribute : ActionFilterAttribute
   {
       public string Message { get; set; }
 
       public override void OnActionExecuted(ActionExecutedContextfilterContext)
       {
           base.OnActionExecuted(filterContext);
           filterContext.HttpContext.Response.Write("
OnActionExecuted \t" + Message); } public override void OnActionExecuting(ActionExecutingContextfilterContext) { base.OnActionExecuting(filterContext); filterContext.HttpContext.Response.Write("
OnActionExecuting\t"+ Message); } public override void OnResultExecuted(ResultExecutedContextfilterContext) { base.OnResultExecuted(filterContext); filterContext.HttpContext.Response.Write("
OnResultExecuted\t" + Message); } public override void OnResultExecuting(ResultExecutingContextfilterContext) { base.OnResultExecuting(filterContext); filterContext.HttpContext.Response.Write("
OnResultExecuting\t" + Message); } } } Action调用: [DemoFilter(Message="IndexAction")] public ActionResult Index() { //ViewData["Message"] = "欢迎使用 ASP.NET MVC!"; //return View(); return Content("
Index Result Excutee"); }


3.MVC(Action返回值),反射,Attribute,Linq加强

(1)      Action返回值
  /// http://localhost:1847/Demo/ContentResultDemo
       /// 
       /// 
       public ActionResult ContentResultDemo()
       {
            string contentString ="ContextResultDemo! 请查看 Controllers/DemoController.cs文件,里面包含所有类型ActionResult的用法.";
           return Content(contentString);
       }
 
       /// 
       /// http://localhost:1847/Demo/EmptyResultDemo
       /// 
       /// 
       public ActionResult EmptyResultDemo()
       {
           return  new EmptyResult();
       }
 
       /// 
       /// http://localhost:1847/Demo/FileContentResultDemo
       /// 
       /// 
       public ActionResult FileContentResultDemo()
       {
           FileStream fs = newFileStream(Server.MapPath(@"/resource/Images/1.gif"), FileMode.Open,FileAccess.Read);
           byte[] buffer = new byte[Convert.ToInt32(fs.Length)];
           fs.Read(buffer, 0, Convert.ToInt32(fs.Length) );
           return File(buffer, @"image/gif");
       }
 
       /// 
       /// http://localhost:1847/Demo/FilePathResultDemo
       /// 
       /// 
       public ActionResult FilePathResultDemo()
       {
           //可以将一个jpg格式的图像输出为gif格式
           return File(Server.MapPath(@"/resource/Images/2.jpg"),@"image/gif");
       }
 
       /// 
       /// http://localhost:1847/Demo/FileStreamResultDemo
       /// 
       /// 
       public ActionResult FileStreamResultDemo()
       {           
           FileStream fs = newFileStream(Server.MapPath(@"/resource/Images/1.gif"), FileMode.Open,FileAccess.Read);
           return File(fs, @"image/gif");
       }
 
       /// 
       /// http://localhost:1847/Demo/HttpUnauthorizedResultDemo
       /// 
       /// 
       public ActionResult HttpUnauthorizedResultDemo()
       {
           return new HttpUnauthorizedResult();
       }
 
       /// 
       /// http://localhost:1847/Demo/JavaScriptResultDemo
       /// 
       /// 
       public ActionResult JavaScriptResultDemo()
       {
           return JavaScript(@"alert(""TestJavaScriptResultDemo!"")");
       }
 
       /// 
       /// http://localhost:1847/Demo/JsonResultDemo
       /// 
       /// 
       public ActionResult JsonResultDemo()
       {
           var tempObj = new { Controller = "DemoController", Action ="JsonResultDemo" };
           return Json(tempObj);
       }
 
       /// 
       /// http://localhost:1847/Demo/RedirectResultDemo
       /// 
       /// 
       public ActionResult RedirectResultDemo()
       {
           returnRedirect(@"http://localhost:1847/Demo/ContentResultDemo");
       }
 
       /// 
       /// http://localhost:1847/Demo/RedirectToRouteResultDemo
       /// 
       /// 
       public ActionResult RedirectToRouteResultDemo()
       {
           return RedirectToAction(@"FileStreamResultDemo");
       }
 
       /// 
       /// http://localhost:1847/Demo/PartialViewResultDemo
       /// 
       /// 
       public ActionResult PartialViewResultDemo()
       {
           return PartialView();
       }
 
       /// 
       /// http://localhost:1847/Demo/ViewResultDemo
       /// 
       /// 
       public ActionResult ViewResultDemo()
       {
           //如果没有传入View名称, 默认寻找与Action名称相同的View页面.
           return View();
        }


    到此为止,asp.net mvc ,非常喜欢,尤其是 linq,下去多多练习!

你可能感兴趣的:(C#)