一:Routes
mvc不是直接访问的页面,而是通过routes映射规则通过action中转,
Routes在Global.asax中RouteConfig里
我们可以修改映射规则,
比如修改起始页:
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
现在defaults里边也就是起始页访问的是名叫Home的controller里边的Index方法,
我们可以把他修改成Home controller里边的About方法
defaults: new { controller = "Home", action = "About", id = UrlParameter.Optional }
一个controller里边可以有多个方法
二:Razor
Razor是MVC3中的一个视图模板引擎 Razor支持两种文件类型,分别是.cshtml 和.vbhtml,其中.cshtml 的服务器代码使用了c#的语法,.vbhtml 的服务器代码使用了vb.net的 语法, 由此也可以看出,Razor其实是一种服务器代码和HTML代码混写的代码模板,类似于没有后置代码的.aspx文件也类似于jsp
输出html
@MvcHtmlString.Create("<th data-options=\"field:'ServicePeopleId',width:100,formatter:function(value,row){ return row.ServicePeopleName;}\">服务人员</th>");
1: js得到razor值:
alert('@ViewBag.count');
alert('@ViewBag.test'); 主要需要加一个单引号
2:遍历
@{
string content = "";
foreach (Dao.DTO_Model.PriceInfo_DTO a in ViewBag.pds as List<Dao.DTO_Model.PriceInfo_DTO>)
{
content += "<tr>";
content += "<td>";
content += "<p class=\"aembox\"><em>aaa</em><a href=\"\"></a></p>";
content += "<div class=\"mpbox\">";
content += "<h2><span>bb</span><i></i></h2>";
}
ViewBag.content = "hello pretty girl";
}
三:使用AJAX
在Controller定义方法
1:直接返回一个json字符串
public string GetBooks()
{
book b = new book();
b.bid = "1";
b.bname = "aaa";
b.bpublisher = "cq";
b.bpubtime = "2013";
b.bauthor = "mm";
DGDate<book> db = new DGDate<book>();
db.total = 1;
db.rows = new List<book>() { b };
return db.ToJson();
}
2:用JsonResult输出
public JsonResult GetBooks2()
{
book b = new book();
b.bid = "1";
b.bname = "aaa";
b.bpublisher = "cq";
b.bpubtime = "2013";
b.bauthor = "mm";
DGDate<book> db = new DGDate<book>();
db.total = 1;
db.rows = new List<book>() { b };
JsonResult jr = new JsonResult();
jr.Data = db;
return jr;
}
前段jquery ajax调用(可见比调用.aspx里边的方法好用多了)
function getdata() {
$.ajax({
type: "post",
url: "http://localhost:1209/home/GetBooks2",
dataType: "json",
success: function (result) {
console.log(result);
}
});
}
前段是供easy ui的datagrid使用
$('#dg').datagrid({
pagination: true,
fitColumns: false,
fit: true,
url: 'http://localhost:1209/home/GetBooks2',
nowrap: true,
sortName: 'bname',
sortOrder: 'desc',
四:controller
1:controler跳转
a:跳转到普通的html页面
Response.Write("<script type='text/javascript'>location.href='../B/F/login.html'</script>");
五:后台返回图片
1:简单图片
public ActionResult GetImage()
{
string path = Server.MapPath("~/UsersInfo/2/Emcee/yw4.jpg");
return File(path, "image/jpeg");
}
<img src="/Emcee/GetImage"/>
2:显示某个文件夹下所有图片
使用个数可以防止文件名暴露
public ActionResult Emcee()
{
System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(Server.MapPath("~/UsersInfo/2/Emcee/"));
List<FileInfo> ff = di.GetFiles("*.jpg").ToList();//di文件夹下全部jpg文件
List<FileInfo> png = di.GetFiles("*.png").ToList();
ff.AddRange(png);
ViewBag.filecounts = ff.Count;
return View("~/Views/Menber/Emcee.cshtml");
}
public ActionResult GetImage(int filecount)
{
System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(Server.MapPath("~/UsersInfo/2/Emcee/"));
List<FileInfo> ff = di.GetFiles("*.jpg").ToList();//di文件夹下全部jpg文件
List<FileInfo> png = di.GetFiles("*.png").ToList();
ff.AddRange(png);
string path = Server.MapPath("~/UsersInfo/2/Emcee/" + ff[filecount].Name);
return File(path, "image/jpeg");
}
前台:
@{
int filecounts = ViewBag.filecounts;
for (int i = 0; i < filecounts;i++ )
{
@MvcHtmlString.Create("<img src=\"/Emcee/GetImage?filecount="+i+"\"/>");
}
}