一、创建一个 ASP.NET MVC 2 空项目
1、修改Global.asax.cs文件
在
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
后添加:
routes.IgnoreRoute("Scripts/{*pathInfo}");
2、添加控制器 HomeController
并添加如何方法:
public string GetTime()
{
return DateTime.Now.ToString();
}
public string GetTime()
{
return DateTime.Now.ToString();
}
3、在Views中添加Home文件夹,并添加Index视图文件,内容如下:
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="
http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Index</title>
<script language="javascript" type="text/javascript" src="/Scripts/MicrosoftAjax.debug.js"></script>
<script language="javascript" type="text/javascript" src="/Scripts/MicrosoftMvcAjax.debug.js"></script>
</head>
<body>
<div>
<%=System.DateTime.Now.ToString() %>
<br />
<span id="time"></span>
<br />
<%= Ajax.ActionLink("Get Time","GetTime", new AjaxOptions{UpdateTargetId="time"}) %>
</div>
</body>
</html>
<head runat="server">
<title>Index</title>
<script language="javascript" type="text/javascript" src="/Scripts/MicrosoftAjax.debug.js"></script>
<script language="javascript" type="text/javascript" src="/Scripts/MicrosoftMvcAjax.debug.js"></script>
</head>
<body>
<div>
<%=System.DateTime.Now.ToString() %>
<br />
<span id="time"></span>
<br />
<%= Ajax.ActionLink("Get Time","GetTime", new AjaxOptions{UpdateTargetId="time"}) %>
</div>
</body>
</html>
4、在IE的访问,点击连接