Asp.Net MVC 学习心得 之 View
2009-03-09 15:56
513 查看
一、使用ViewData
首先要在Controller里加代码:
然后在View里添加代码:
内容就被传递了,但这样由于传入的值可能是任何类型的,所以你要自己做类型转换了,哈哈。
ViewData.Model是可以让你有个强类型的
ViewData.Model=_dataModel.ProductSet.ToList();
二、预防javascript注入攻击
默认是预防的,如果不想使用看下面的代码:
主要是那个[ValidateInput(false)]
三、可替换的ViewEngine
Asp.netMvc里的ViewEngine是可以替换的,默认的engine是叫做WebFormsViewEngine.现在已经有几个开源的ViewEngine了
http://www.codeplex.com/MVCContrib可以到这里找找看,一个网站可以同时使用几个ViewEngine,可以在Global.asax里配置,使用不同的扩展名来区分使用不同ViewEngine比如:.aspx页面就使用WebFormsViewEngine.
我们也可以自定义一个ViewEngine,从VirtualPathProviderViewEngine继承出一个类就可以了。
然后自己写一个view的页面类:
定义一个Controller:
而页面可以写成:Views\Simple\Index.simple
主要看{message}被解析出来了
注:上面的代码是从别人书里炒来的,不好意思
在View里有个HtmlHelper的东西以后再写。
首先要在Controller里加代码:
publicclassProductController:Controller { // //GET:/Product/ publicActionResultIndex() { ViewData["Message"]="HelloWorld"; returnView(); } }
然后在View里添加代码:
<%@PageTitle=""Language="C#"MasterPageFile="~/Views/Shared/Site.Master"Inherits="System.Web.Mvc.ViewPage<ToyStore.Models.Products>"%> <asp:ContentID="Content1"ContentPlaceHolderID="TitleContent"runat="server"> Index </asp:Content> <asp:ContentID="Content2"ContentPlaceHolderID="MainContent"runat="server"> <h2>Index</h2> <%=ViewData["Message"]%> </asp:Content>
内容就被传递了,但这样由于传入的值可能是任何类型的,所以你要自己做类型转换了,哈哈。
ViewData.Model是可以让你有个强类型的
ViewData.Model=_dataModel.ProductSet.ToList();
二、预防javascript注入攻击
默认是预防的,如果不想使用看下面的代码:
[ValidateInput(false)] [AcceptVerbs(HttpVerbs.Post)] publicActionResultCreate([Bind(Exclude="Id")]ProductproductToCreate) { if(!ModelState.IsValid) returnView(); try { _dataModel.AddToProductSet(productToCreate); _dataModel.SaveChanges(); returnRedirectToAction("Index"); } catch { returnView(); } }
主要是那个[ValidateInput(false)]
三、可替换的ViewEngine
Asp.netMvc里的ViewEngine是可以替换的,默认的engine是叫做WebFormsViewEngine.现在已经有几个开源的ViewEngine了
我们也可以自定义一个ViewEngine,从VirtualPathProviderViewEngine继承出一个类就可以了。
usingSystem.Web.Mvc; namespaceMvcApplication1.MyViewEngines { publicclassSimpleViewEngine:VirtualPathProviderViewEngine { publicSimpleViewEngine() { this.ViewLocationFormats=newstring[]{"~/Views/{1}/{0}.simple","~/Views/Shared/{0}.simple"}; this.PartialViewLocationFormats=newstring[]{"~/Views/{1}/{0}.simple","~/Views/Shared/{0}.simple"}; } protectedoverrideIViewCreateView(ControllerContextcontrollerContext,stringviewPath,stringmasterPath) { varphysicalPath=controllerContext.HttpContext.Server.MapPath(viewPath); returnnewSimpleView(physicalPath); } protectedoverrideIViewCreatePartialView(ControllerContextcontrollerContext,stringpartialPath) { varphysicalPath=controllerContext.HttpContext.Server.MapPath(partialPath); returnnewSimpleView(physicalPath); } } }
在Global.asax里这样写
protectedvoidApplication_Start()
{
RegisterRoutes(RouteTable.Routes);
ViewEngines.Engines.Add(newSimpleViewEngine());
}
然后自己写一个view的页面类:
namespaceMvcApplication1.MyViewEngines
{
publicclassSimpleView:IView
{
privatestring_viewPhysicalPath;
publicSimpleView(stringviewPhysicalPath)
{
_viewPhysicalPath=viewPhysicalPath;
}
#regionIViewMembers
publicvoidRender(ViewContextviewContext,TextWriterwriter)
{
//Loadfile
stringrawContents=File.ReadAllText(_viewPhysicalPath);
//Performreplacements
stringparsedContents=Parse(rawContents,viewContext.ViewData);
//WriteresultstoHttpContext
writer.Write(parsedContents);
}
#endregion
publicstringParse(stringcontents,ViewDataDictionaryviewData)
{
returnRegex.Replace(contents,"\\{(.+)\\}",m=>GetMatch(m,viewData));
}
protectedvirtualstringGetMatch(Matchm,ViewDataDictionaryviewData)
{
if(m.Success)
{
stringkey=m.Result("$1");
if(viewData.ContainsKey(key))
{
returnviewData[key].ToString();
}
}
returnstring.Empty;
}
}
}
定义一个Controller:
usingSystem.Web.Mvc;
namespaceMvcApplication1.Controllers
{
publicclassSimpleController:Controller
{
publicActionResultIndex()
{
ViewData["message"]="HelloWorld!";
returnView();
}
}
}
而页面可以写成:Views\Simple\Index.simple
<html>
<head><title>IndexSimpleView</title></head>
<body>
<h1>{message}</h1>
</body>
</html>
主要看{message}被解析出来了
注:上面的代码是从别人书里炒来的,不好意思
在View里有个HtmlHelper的东西以后再写。
相关文章推荐
- Asp.Net MVC 学习心得 之 Html Helper
- ASP.NET MVC 学习笔记-5.Controller与View的数据传递
- 【jqGrid for ASP.NET MVC Documentation】.学习笔记.2.jqGrid Model-View-Controller 分离
- ASP.Net MVC开发基础学习笔记(10):分部视图PartialView
- ASP.NET MVC学习笔记:(二)return View(...)
- ASP.NET MVC 学习心得 (4) – 视图控制器
- 从零开始学习 ASP.NET MVC 1.0 (四) View/Model 全解
- ASP.NET MVC学习笔记-----使用自定义的View Engine
- [学习心得][Introduction to ASP.NET Core 1.0]3-2 ASP.NET Core and MVC Pattern
- ASP.NET MVC 学习心得 (2) - 怎样创建自定义URL Routing
- 从零开始学习 ASP.NET MVC 1.0 (五) ViewEngine 深入解析与应用实例
- Asp.Net MVC 学习心得 之 Model
- asp.net mvc学习笔记1(razor,html,url辅助器,ViewData,ViewBag)
- ASP.NET MVC 学习2、从Controller传递数据到View
- ASP.NET MVC学习笔记-Controller与View传值
- ASP.NET MVC学习笔记-Controller与View传值
- 从零开始学习 ASP.NET MVC 1.0 (五) ViewEngine 深入解析与应用实例
- 从零开始学习 ASP.NET MVC 1.0 (四) View/Model 全解 【转】
- 从零开始学习 ASP.NET MVC 1.0 (四) View/Model 全解
- ASP.NET MVC学习笔记-Controller与View传值