您的位置:首页 > 编程语言 > ASP

Asp.Net MVC 学习心得 之 View

2009-03-09 15:56 513 查看
一、使用ViewData

首先要在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了

http://www.codeplex.com/MVCContrib可以到这里找找看,一个网站可以同时使用几个ViewEngine,可以在Global.asax里配置,使用不同的扩展名来区分使用不同ViewEngine比如:.aspx页面就使用WebFormsViewEngine.

我们也可以自定义一个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的东西以后再写。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: