XSLTテンプレートは、1つ以上の入力XMLドキュメントの出力への変換を記述するXMLドキュメントです。通常、これもXMLまたはHTMLドキュメントです。 この場合、テンプレートの入力は、対応するモデルから取得したデータのXML表現になります。 テンプレートに転送されたデータは、ページコンテンツの生成に使用されます。
主なプロジェクトは、ASP.NET MVC 3アプリケーションテンプレートです。 ここでは、ビューエンジンの選択は重要ではありません。
データモデル
リプレゼンテーションに渡されるすべてのモデルについて、次の基本クラスを説明します。
[ Serializable ]
[XmlInclude( typeof (UserModel))]
public abstract class XmlModel { }
このクラスのオブジェクトはXMLでシリアル化されるため、最初の属性が必要です。 2番目の属性を使用する理由については、以下で説明します。
UserModelは、ビューに渡す子クラスの例です。
[ Serializable ]
public class UserModel : XmlModel
{
[ XmlAttribute ]
public int UserId { get ; set ; }
public string Login { get ; set ; }
public string Email { get ; set ; }
}
次に、シリアル化されてテンプレートに渡されるメインクラスについて説明します。
[ Serializable ]
public class Result
{
[ XmlAttribute ]
public string Controller { get ; set ; }
[ XmlAttribute ]
public string Action { get ; set ; }
public List Data { get ; set ; }
}
, , .
Data XmlModel, , XmlInclude XmlModel.
XML- :
< Result Controller ="SomeController" Action ="SomeAction" >
< Data >
<!-- -->
</ Data >
</ Result >
ActionResult. , . c XML-, :
private string _data;
public XmlResult( string data)
{
_data = data;
}
, - ExecuteResult. . :
public override void ExecuteResult(ControllerContext controllerContext)
{
var xslTransform = new XslCompiledTransform();
//
var controller = controllerContext.RouteData.Values[ "controller" ] as string ;
var action = controllerContext.RouteData.Values[ "action" ] as string ;
var templatePath = string .Format( "~/Views/{0}/{1}.xsl" , controller, action);
var virtualFile = HostingEnvironment.VirtualPathProvider.GetFile(templatePath);
// :
//var virtualFile = HostingEnvironment.VirtualPathProvider.GetFile("~/Views/default.xsl");
var xmlReader = XmlReader.Create(virtualFile.Open());
xslTransform.Load(xmlReader);
xmlReader.Close();
var stringReader = new StringReader (_data);
xmlReader = XmlReader.Create(stringReader);
// XML- http-
var xmlWriter = XmlWriter.Create(controllerContext. HttpContext .Response.OutputStream);
xslTransform.Transform(xmlReader, xmlWriter);
stringReader.Close();
xmlReader.Close();
xmlWriter.Close();
}
:
public class XmlResult : ActionResult
{
private string _data;
public XmlResult( string data)
{
_data = data;
}
public override void ExecuteResult(ControllerContext controllerContext)
{
var xslTransform = new XslCompiledTransform();
//
var controller = controllerContext.RouteData.Values[ "controller" ] as string ;
var action = controllerContext.RouteData.Values[ "action" ] as string ;
var templatePath = string .Format( "~/Views/{0}/{1}.xsl" , controller, action);
var virtualFile = HostingEnvironment.VirtualPathProvider.GetFile(templatePath);
// :
//var virtualFile = HostingEnvironment.VirtualPathProvider.GetFile("~/Views/default.xsl");
var xmlReader = XmlReader.Create(virtualFile.Open());
xslTransform.Load(xmlReader);
xmlReader.Close();
var stringReader = new StringReader (_data);
xmlReader = XmlReader.Create(stringReader);
// XML- http-
var xmlWriter = XmlWriter.Create(controllerContext. HttpContext .Response.OutputStream);
xslTransform.Transform(xmlReader, xmlWriter);
stringReader.Close();
xmlReader.Close();
xmlWriter.Close();
}
}
.
ViewData, :
public new List ViewData = new List ();
View, ActionResult . :
public new ActionResult View()
{
//
var result = new Result
{
Controller = ControllerContext.RouteData.Values[ "controller" ] as string ,
Action = ControllerContext.RouteData.Values[ "action" ] as string ,
Data = ViewData
};
var xmlSerializer = new XmlSerializer( typeof (Result));
var stringWriter = new StringWriter ();
xmlSerializer.Serialize(stringWriter, result);
stringWriter.Close();
// ActionResult
return new XmlResult(stringWriter.ToString());
}
:
public abstract class XsltController : Controller
{
public new List ViewData = new List ();
public new ActionResult View()
{
//
var result = new Result
{
Controller = ControllerContext.RouteData.Values[ "controller" ] as string ,
Action = ControllerContext.RouteData.Values[ "action" ] as string ,
Data = ViewData
};
var xmlSerializer = new XmlSerializer( typeof (Result));
var stringWriter = new StringWriter ();
xmlSerializer.Serialize(stringWriter, result);
stringWriter.Close();
// ActionResult
return new XmlResult(stringWriter.ToString());
}
}
:
public class HomeController : XsltController
{
public ActionResult Index()
{
var user = new UserModel { UserId = 1, Login = "root" , Email = "noreply@localhost" };
ViewData.Add(user);
return View();
}
}
. XML:
<? xml version ="1.0" encoding ="utf-16" ? >
< Result xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd ="http://www.w3.org/2001/XMLSchema" Controller ="Home" Action ="Index" >
< Data >
< XmlModel xsi:type ="UserModel" UserId ="1" >
< Login > root </ Login >
< Email > noreply@localhost </ Email >
</ XmlModel >
</ Data >
</ Result >
XSLT-, :
<? xml version ="1.0" encoding ="utf-8" ? >
< xsl:stylesheet version ="1.0" xmlns:xsl ="http://www.w3.org/1999/XSL/Transform" xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" >
< xsl:output encoding ="utf-8"
method ="xml"
media-type ="application/xhtml+xml"
doctype-public ="-//W3C//DTD XHTML 1.1//EN"
doctype-system ="http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd" />
< xsl:template match ="/" >
< html xmlns ="http://www.w3.org/1999/xhtml" >
< head >
< meta http-equiv ="Content-Type" content ="application/xhtml+xml; charset=utf-8" />
< title > XSLT + MVC </ title >
</ head >
< body >
< p > Controller: < xsl:value-of select ="/Result/@Controller" /></ p >
< p > Action: < xsl:value-of select ="/Result/@Action" /></ p >
< p > User: < xsl:value-of select ="//XmlModel[@xsi:type = 'UserModel']/Login" /></ p >
</ body >
</ html >
</ xsl:template >
</ xsl:stylesheet >
HTML-:
<? xml version ="1.0" encoding ="utf-8" ? >
< html xmlns ="http://www.w3.org/1999/xhtml" xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" >
< head >
< meta http-equiv ="Content-Type" content ="application/xhtml+xml; charset=utf-8" />
< title > XSLT + MVC </ title >
</ head >
< body >
< p > Controller: Home </ p >
< p > Action: Index </ p >
< p > User: root </ p >
</ body >
</ html >
, , , . Controller Action , .
* This source code was highlighted with Source Code Highlighter .
[ Serializable ]
public class Result
{
[ XmlAttribute ]
public string Controller { get ; set ; }
[ XmlAttribute ]
public string Action { get ; set ; }
public List Data { get ; set ; }
}
, , .
Data XmlModel, , XmlInclude XmlModel.
XML- :
< Result Controller ="SomeController" Action ="SomeAction" >
< Data >
<!-- -->
</ Data >
</ Result >
ActionResult. , . c XML-, :
private string _data;
public XmlResult( string data)
{
_data = data;
}
, - ExecuteResult. . :
public override void ExecuteResult(ControllerContext controllerContext)
{
var xslTransform = new XslCompiledTransform();
//
var controller = controllerContext.RouteData.Values[ "controller" ] as string ;
var action = controllerContext.RouteData.Values[ "action" ] as string ;
var templatePath = string .Format( "~/Views/{0}/{1}.xsl" , controller, action);
var virtualFile = HostingEnvironment.VirtualPathProvider.GetFile(templatePath);
// :
//var virtualFile = HostingEnvironment.VirtualPathProvider.GetFile("~/Views/default.xsl");
var xmlReader = XmlReader.Create(virtualFile.Open());
xslTransform.Load(xmlReader);
xmlReader.Close();
var stringReader = new StringReader (_data);
xmlReader = XmlReader.Create(stringReader);
// XML- http-
var xmlWriter = XmlWriter.Create(controllerContext. HttpContext .Response.OutputStream);
xslTransform.Transform(xmlReader, xmlWriter);
stringReader.Close();
xmlReader.Close();
xmlWriter.Close();
}
:
public class XmlResult : ActionResult
{
private string _data;
public XmlResult( string data)
{
_data = data;
}
public override void ExecuteResult(ControllerContext controllerContext)
{
var xslTransform = new XslCompiledTransform();
//
var controller = controllerContext.RouteData.Values[ "controller" ] as string ;
var action = controllerContext.RouteData.Values[ "action" ] as string ;
var templatePath = string .Format( "~/Views/{0}/{1}.xsl" , controller, action);
var virtualFile = HostingEnvironment.VirtualPathProvider.GetFile(templatePath);
// :
//var virtualFile = HostingEnvironment.VirtualPathProvider.GetFile("~/Views/default.xsl");
var xmlReader = XmlReader.Create(virtualFile.Open());
xslTransform.Load(xmlReader);
xmlReader.Close();
var stringReader = new StringReader (_data);
xmlReader = XmlReader.Create(stringReader);
// XML- http-
var xmlWriter = XmlWriter.Create(controllerContext. HttpContext .Response.OutputStream);
xslTransform.Transform(xmlReader, xmlWriter);
stringReader.Close();
xmlReader.Close();
xmlWriter.Close();
}
}
.
ViewData, :
public new List ViewData = new List ();
View, ActionResult . :
public new ActionResult View()
{
//
var result = new Result
{
Controller = ControllerContext.RouteData.Values[ "controller" ] as string ,
Action = ControllerContext.RouteData.Values[ "action" ] as string ,
Data = ViewData
};
var xmlSerializer = new XmlSerializer( typeof (Result));
var stringWriter = new StringWriter ();
xmlSerializer.Serialize(stringWriter, result);
stringWriter.Close();
// ActionResult
return new XmlResult(stringWriter.ToString());
}
:
public abstract class XsltController : Controller
{
public new List ViewData = new List ();
public new ActionResult View()
{
//
var result = new Result
{
Controller = ControllerContext.RouteData.Values[ "controller" ] as string ,
Action = ControllerContext.RouteData.Values[ "action" ] as string ,
Data = ViewData
};
var xmlSerializer = new XmlSerializer( typeof (Result));
var stringWriter = new StringWriter ();
xmlSerializer.Serialize(stringWriter, result);
stringWriter.Close();
// ActionResult
return new XmlResult(stringWriter.ToString());
}
}
:
public class HomeController : XsltController
{
public ActionResult Index()
{
var user = new UserModel { UserId = 1, Login = "root" , Email = "noreply@localhost" };
ViewData.Add(user);
return View();
}
}
. XML:
<? xml version ="1.0" encoding ="utf-16" ? >
< Result xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd ="http://www.w3.org/2001/XMLSchema" Controller ="Home" Action ="Index" >
< Data >
< XmlModel xsi:type ="UserModel" UserId ="1" >
< Login > root </ Login >
< Email > noreply@localhost </ Email >
</ XmlModel >
</ Data >
</ Result >
XSLT-, :
<? xml version ="1.0" encoding ="utf-8" ? >
< xsl:stylesheet version ="1.0" xmlns:xsl ="http://www.w3.org/1999/XSL/Transform" xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" >
< xsl:output encoding ="utf-8"
method ="xml"
media-type ="application/xhtml+xml"
doctype-public ="-//W3C//DTD XHTML 1.1//EN"
doctype-system ="http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd" />
< xsl:template match ="/" >
< html xmlns ="http://www.w3.org/1999/xhtml" >
< head >
< meta http-equiv ="Content-Type" content ="application/xhtml+xml; charset=utf-8" />
< title > XSLT + MVC </ title >
</ head >
< body >
< p > Controller: < xsl:value-of select ="/Result/@Controller" /></ p >
< p > Action: < xsl:value-of select ="/Result/@Action" /></ p >
< p > User: < xsl:value-of select ="//XmlModel[@xsi:type = 'UserModel']/Login" /></ p >
</ body >
</ html >
</ xsl:template >
</ xsl:stylesheet >
HTML-:
<? xml version ="1.0" encoding ="utf-8" ? >
< html xmlns ="http://www.w3.org/1999/xhtml" xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" >
< head >
< meta http-equiv ="Content-Type" content ="application/xhtml+xml; charset=utf-8" />
< title > XSLT + MVC </ title >
</ head >
< body >
< p > Controller: Home </ p >
< p > Action: Index </ p >
< p > User: root </ p >
</ body >
</ html >
, , , . Controller Action , .
* This source code was highlighted with Source Code Highlighter .
[ Serializable ]
public class Result
{
[ XmlAttribute ]
public string Controller { get ; set ; }
[ XmlAttribute ]
public string Action { get ; set ; }
public List Data { get ; set ; }
}
, , .
Data XmlModel, , XmlInclude XmlModel.
XML- :
< Result Controller ="SomeController" Action ="SomeAction" >
< Data >
<!-- -->
</ Data >
</ Result >
ActionResult. , . c XML-, :
private string _data;
public XmlResult( string data)
{
_data = data;
}
, - ExecuteResult. . :
public override void ExecuteResult(ControllerContext controllerContext)
{
var xslTransform = new XslCompiledTransform();
//
var controller = controllerContext.RouteData.Values[ "controller" ] as string ;
var action = controllerContext.RouteData.Values[ "action" ] as string ;
var templatePath = string .Format( "~/Views/{0}/{1}.xsl" , controller, action);
var virtualFile = HostingEnvironment.VirtualPathProvider.GetFile(templatePath);
// :
//var virtualFile = HostingEnvironment.VirtualPathProvider.GetFile("~/Views/default.xsl");
var xmlReader = XmlReader.Create(virtualFile.Open());
xslTransform.Load(xmlReader);
xmlReader.Close();
var stringReader = new StringReader (_data);
xmlReader = XmlReader.Create(stringReader);
// XML- http-
var xmlWriter = XmlWriter.Create(controllerContext. HttpContext .Response.OutputStream);
xslTransform.Transform(xmlReader, xmlWriter);
stringReader.Close();
xmlReader.Close();
xmlWriter.Close();
}
:
public class XmlResult : ActionResult
{
private string _data;
public XmlResult( string data)
{
_data = data;
}
public override void ExecuteResult(ControllerContext controllerContext)
{
var xslTransform = new XslCompiledTransform();
//
var controller = controllerContext.RouteData.Values[ "controller" ] as string ;
var action = controllerContext.RouteData.Values[ "action" ] as string ;
var templatePath = string .Format( "~/Views/{0}/{1}.xsl" , controller, action);
var virtualFile = HostingEnvironment.VirtualPathProvider.GetFile(templatePath);
// :
//var virtualFile = HostingEnvironment.VirtualPathProvider.GetFile("~/Views/default.xsl");
var xmlReader = XmlReader.Create(virtualFile.Open());
xslTransform.Load(xmlReader);
xmlReader.Close();
var stringReader = new StringReader (_data);
xmlReader = XmlReader.Create(stringReader);
// XML- http-
var xmlWriter = XmlWriter.Create(controllerContext. HttpContext .Response.OutputStream);
xslTransform.Transform(xmlReader, xmlWriter);
stringReader.Close();
xmlReader.Close();
xmlWriter.Close();
}
}
.
ViewData, :
public new List ViewData = new List ();
View, ActionResult . :
public new ActionResult View()
{
//
var result = new Result
{
Controller = ControllerContext.RouteData.Values[ "controller" ] as string ,
Action = ControllerContext.RouteData.Values[ "action" ] as string ,
Data = ViewData
};
var xmlSerializer = new XmlSerializer( typeof (Result));
var stringWriter = new StringWriter ();
xmlSerializer.Serialize(stringWriter, result);
stringWriter.Close();
// ActionResult
return new XmlResult(stringWriter.ToString());
}
:
public abstract class XsltController : Controller
{
public new List ViewData = new List ();
public new ActionResult View()
{
//
var result = new Result
{
Controller = ControllerContext.RouteData.Values[ "controller" ] as string ,
Action = ControllerContext.RouteData.Values[ "action" ] as string ,
Data = ViewData
};
var xmlSerializer = new XmlSerializer( typeof (Result));
var stringWriter = new StringWriter ();
xmlSerializer.Serialize(stringWriter, result);
stringWriter.Close();
// ActionResult
return new XmlResult(stringWriter.ToString());
}
}
:
public class HomeController : XsltController
{
public ActionResult Index()
{
var user = new UserModel { UserId = 1, Login = "root" , Email = "noreply@localhost" };
ViewData.Add(user);
return View();
}
}
. XML:
<? xml version ="1.0" encoding ="utf-16" ? >
< Result xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd ="http://www.w3.org/2001/XMLSchema" Controller ="Home" Action ="Index" >
< Data >
< XmlModel xsi:type ="UserModel" UserId ="1" >
< Login > root </ Login >
< Email > noreply@localhost </ Email >
</ XmlModel >
</ Data >
</ Result >
XSLT-, :
<? xml version ="1.0" encoding ="utf-8" ? >
< xsl:stylesheet version ="1.0" xmlns:xsl ="http://www.w3.org/1999/XSL/Transform" xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" >
< xsl:output encoding ="utf-8"
method ="xml"
media-type ="application/xhtml+xml"
doctype-public ="-//W3C//DTD XHTML 1.1//EN"
doctype-system ="http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd" />
< xsl:template match ="/" >
< html xmlns ="http://www.w3.org/1999/xhtml" >
< head >
< meta http-equiv ="Content-Type" content ="application/xhtml+xml; charset=utf-8" />
< title > XSLT + MVC </ title >
</ head >
< body >
< p > Controller: < xsl:value-of select ="/Result/@Controller" /></ p >
< p > Action: < xsl:value-of select ="/Result/@Action" /></ p >
< p > User: < xsl:value-of select ="//XmlModel[@xsi:type = 'UserModel']/Login" /></ p >
</ body >
</ html >
</ xsl:template >
</ xsl:stylesheet >
HTML-:
<? xml version ="1.0" encoding ="utf-8" ? >
< html xmlns ="http://www.w3.org/1999/xhtml" xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" >
< head >
< meta http-equiv ="Content-Type" content ="application/xhtml+xml; charset=utf-8" />
< title > XSLT + MVC </ title >
</ head >
< body >
< p > Controller: Home </ p >
< p > Action: Index </ p >
< p > User: root </ p >
</ body >
</ html >
, , , . Controller Action , .
* This source code was highlighted with Source Code Highlighter .
[ Serializable ]
public class Result
{
[ XmlAttribute ]
public string Controller { get ; set ; }
[ XmlAttribute ]
public string Action { get ; set ; }
public List Data { get ; set ; }
}
, , .
Data XmlModel, , XmlInclude XmlModel.
XML- :
< Result Controller ="SomeController" Action ="SomeAction" >
< Data >
<!-- -->
</ Data >
</ Result >
ActionResult. , . c XML-, :
private string _data;
public XmlResult( string data)
{
_data = data;
}
, - ExecuteResult. . :
public override void ExecuteResult(ControllerContext controllerContext)
{
var xslTransform = new XslCompiledTransform();
//
var controller = controllerContext.RouteData.Values[ "controller" ] as string ;
var action = controllerContext.RouteData.Values[ "action" ] as string ;
var templatePath = string .Format( "~/Views/{0}/{1}.xsl" , controller, action);
var virtualFile = HostingEnvironment.VirtualPathProvider.GetFile(templatePath);
// :
//var virtualFile = HostingEnvironment.VirtualPathProvider.GetFile("~/Views/default.xsl");
var xmlReader = XmlReader.Create(virtualFile.Open());
xslTransform.Load(xmlReader);
xmlReader.Close();
var stringReader = new StringReader (_data);
xmlReader = XmlReader.Create(stringReader);
// XML- http-
var xmlWriter = XmlWriter.Create(controllerContext. HttpContext .Response.OutputStream);
xslTransform.Transform(xmlReader, xmlWriter);
stringReader.Close();
xmlReader.Close();
xmlWriter.Close();
}
:
public class XmlResult : ActionResult
{
private string _data;
public XmlResult( string data)
{
_data = data;
}
public override void ExecuteResult(ControllerContext controllerContext)
{
var xslTransform = new XslCompiledTransform();
//
var controller = controllerContext.RouteData.Values[ "controller" ] as string ;
var action = controllerContext.RouteData.Values[ "action" ] as string ;
var templatePath = string .Format( "~/Views/{0}/{1}.xsl" , controller, action);
var virtualFile = HostingEnvironment.VirtualPathProvider.GetFile(templatePath);
// :
//var virtualFile = HostingEnvironment.VirtualPathProvider.GetFile("~/Views/default.xsl");
var xmlReader = XmlReader.Create(virtualFile.Open());
xslTransform.Load(xmlReader);
xmlReader.Close();
var stringReader = new StringReader (_data);
xmlReader = XmlReader.Create(stringReader);
// XML- http-
var xmlWriter = XmlWriter.Create(controllerContext. HttpContext .Response.OutputStream);
xslTransform.Transform(xmlReader, xmlWriter);
stringReader.Close();
xmlReader.Close();
xmlWriter.Close();
}
}
.
ViewData, :
public new List ViewData = new List ();
View, ActionResult . :
public new ActionResult View()
{
//
var result = new Result
{
Controller = ControllerContext.RouteData.Values[ "controller" ] as string ,
Action = ControllerContext.RouteData.Values[ "action" ] as string ,
Data = ViewData
};
var xmlSerializer = new XmlSerializer( typeof (Result));
var stringWriter = new StringWriter ();
xmlSerializer.Serialize(stringWriter, result);
stringWriter.Close();
// ActionResult
return new XmlResult(stringWriter.ToString());
}
:
public abstract class XsltController : Controller
{
public new List ViewData = new List ();
public new ActionResult View()
{
//
var result = new Result
{
Controller = ControllerContext.RouteData.Values[ "controller" ] as string ,
Action = ControllerContext.RouteData.Values[ "action" ] as string ,
Data = ViewData
};
var xmlSerializer = new XmlSerializer( typeof (Result));
var stringWriter = new StringWriter ();
xmlSerializer.Serialize(stringWriter, result);
stringWriter.Close();
// ActionResult
return new XmlResult(stringWriter.ToString());
}
}
:
public class HomeController : XsltController
{
public ActionResult Index()
{
var user = new UserModel { UserId = 1, Login = "root" , Email = "noreply@localhost" };
ViewData.Add(user);
return View();
}
}
. XML:
<? xml version ="1.0" encoding ="utf-16" ? >
< Result xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd ="http://www.w3.org/2001/XMLSchema" Controller ="Home" Action ="Index" >
< Data >
< XmlModel xsi:type ="UserModel" UserId ="1" >
< Login > root </ Login >
< Email > noreply@localhost </ Email >
</ XmlModel >
</ Data >
</ Result >
XSLT-, :
<? xml version ="1.0" encoding ="utf-8" ? >
< xsl:stylesheet version ="1.0" xmlns:xsl ="http://www.w3.org/1999/XSL/Transform" xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" >
< xsl:output encoding ="utf-8"
method ="xml"
media-type ="application/xhtml+xml"
doctype-public ="-//W3C//DTD XHTML 1.1//EN"
doctype-system ="http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd" />
< xsl:template match ="/" >
< html xmlns ="http://www.w3.org/1999/xhtml" >
< head >
< meta http-equiv ="Content-Type" content ="application/xhtml+xml; charset=utf-8" />
< title > XSLT + MVC </ title >
</ head >
< body >
< p > Controller: < xsl:value-of select ="/Result/@Controller" /></ p >
< p > Action: < xsl:value-of select ="/Result/@Action" /></ p >
< p > User: < xsl:value-of select ="//XmlModel[@xsi:type = 'UserModel']/Login" /></ p >
</ body >
</ html >
</ xsl:template >
</ xsl:stylesheet >
HTML-:
<? xml version ="1.0" encoding ="utf-8" ? >
< html xmlns ="http://www.w3.org/1999/xhtml" xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" >
< head >
< meta http-equiv ="Content-Type" content ="application/xhtml+xml; charset=utf-8" />
< title > XSLT + MVC </ title >
</ head >
< body >
< p > Controller: Home </ p >
< p > Action: Index </ p >
< p > User: root </ p >
</ body >
</ html >
, , , . Controller Action , .
* This source code was highlighted with Source Code Highlighter .
[ Serializable ]
public class Result
{
[ XmlAttribute ]
public string Controller { get ; set ; }
[ XmlAttribute ]
public string Action { get ; set ; }
public List Data { get ; set ; }
}
, , .
Data XmlModel, , XmlInclude XmlModel.
XML- :
< Result Controller ="SomeController" Action ="SomeAction" >
< Data >
<!-- -->
</ Data >
</ Result >
ActionResult. , . c XML-, :
private string _data;
public XmlResult( string data)
{
_data = data;
}
, - ExecuteResult. . :
public override void ExecuteResult(ControllerContext controllerContext)
{
var xslTransform = new XslCompiledTransform();
//
var controller = controllerContext.RouteData.Values[ "controller" ] as string ;
var action = controllerContext.RouteData.Values[ "action" ] as string ;
var templatePath = string .Format( "~/Views/{0}/{1}.xsl" , controller, action);
var virtualFile = HostingEnvironment.VirtualPathProvider.GetFile(templatePath);
// :
//var virtualFile = HostingEnvironment.VirtualPathProvider.GetFile("~/Views/default.xsl");
var xmlReader = XmlReader.Create(virtualFile.Open());
xslTransform.Load(xmlReader);
xmlReader.Close();
var stringReader = new StringReader (_data);
xmlReader = XmlReader.Create(stringReader);
// XML- http-
var xmlWriter = XmlWriter.Create(controllerContext. HttpContext .Response.OutputStream);
xslTransform.Transform(xmlReader, xmlWriter);
stringReader.Close();
xmlReader.Close();
xmlWriter.Close();
}
:
public class XmlResult : ActionResult
{
private string _data;
public XmlResult( string data)
{
_data = data;
}
public override void ExecuteResult(ControllerContext controllerContext)
{
var xslTransform = new XslCompiledTransform();
//
var controller = controllerContext.RouteData.Values[ "controller" ] as string ;
var action = controllerContext.RouteData.Values[ "action" ] as string ;
var templatePath = string .Format( "~/Views/{0}/{1}.xsl" , controller, action);
var virtualFile = HostingEnvironment.VirtualPathProvider.GetFile(templatePath);
// :
//var virtualFile = HostingEnvironment.VirtualPathProvider.GetFile("~/Views/default.xsl");
var xmlReader = XmlReader.Create(virtualFile.Open());
xslTransform.Load(xmlReader);
xmlReader.Close();
var stringReader = new StringReader (_data);
xmlReader = XmlReader.Create(stringReader);
// XML- http-
var xmlWriter = XmlWriter.Create(controllerContext. HttpContext .Response.OutputStream);
xslTransform.Transform(xmlReader, xmlWriter);
stringReader.Close();
xmlReader.Close();
xmlWriter.Close();
}
}
.
ViewData, :
public new List ViewData = new List ();
View, ActionResult . :
public new ActionResult View()
{
//
var result = new Result
{
Controller = ControllerContext.RouteData.Values[ "controller" ] as string ,
Action = ControllerContext.RouteData.Values[ "action" ] as string ,
Data = ViewData
};
var xmlSerializer = new XmlSerializer( typeof (Result));
var stringWriter = new StringWriter ();
xmlSerializer.Serialize(stringWriter, result);
stringWriter.Close();
// ActionResult
return new XmlResult(stringWriter.ToString());
}
:
public abstract class XsltController : Controller
{
public new List ViewData = new List ();
public new ActionResult View()
{
//
var result = new Result
{
Controller = ControllerContext.RouteData.Values[ "controller" ] as string ,
Action = ControllerContext.RouteData.Values[ "action" ] as string ,
Data = ViewData
};
var xmlSerializer = new XmlSerializer( typeof (Result));
var stringWriter = new StringWriter ();
xmlSerializer.Serialize(stringWriter, result);
stringWriter.Close();
// ActionResult
return new XmlResult(stringWriter.ToString());
}
}
:
public class HomeController : XsltController
{
public ActionResult Index()
{
var user = new UserModel { UserId = 1, Login = "root" , Email = "noreply@localhost" };
ViewData.Add(user);
return View();
}
}
. XML:
<? xml version ="1.0" encoding ="utf-16" ? >
< Result xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd ="http://www.w3.org/2001/XMLSchema" Controller ="Home" Action ="Index" >
< Data >
< XmlModel xsi:type ="UserModel" UserId ="1" >
< Login > root </ Login >
< Email > noreply@localhost </ Email >
</ XmlModel >
</ Data >
</ Result >
XSLT-, :
<? xml version ="1.0" encoding ="utf-8" ? >
< xsl:stylesheet version ="1.0" xmlns:xsl ="http://www.w3.org/1999/XSL/Transform" xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" >
< xsl:output encoding ="utf-8"
method ="xml"
media-type ="application/xhtml+xml"
doctype-public ="-//W3C//DTD XHTML 1.1//EN"
doctype-system ="http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd" />
< xsl:template match ="/" >
< html xmlns ="http://www.w3.org/1999/xhtml" >
< head >
< meta http-equiv ="Content-Type" content ="application/xhtml+xml; charset=utf-8" />
< title > XSLT + MVC </ title >
</ head >
< body >
< p > Controller: < xsl:value-of select ="/Result/@Controller" /></ p >
< p > Action: < xsl:value-of select ="/Result/@Action" /></ p >
< p > User: < xsl:value-of select ="//XmlModel[@xsi:type = 'UserModel']/Login" /></ p >
</ body >
</ html >
</ xsl:template >
</ xsl:stylesheet >
HTML-:
<? xml version ="1.0" encoding ="utf-8" ? >
< html xmlns ="http://www.w3.org/1999/xhtml" xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" >
< head >
< meta http-equiv ="Content-Type" content ="application/xhtml+xml; charset=utf-8" />
< title > XSLT + MVC </ title >
</ head >
< body >
< p > Controller: Home </ p >
< p > Action: Index </ p >
< p > User: root </ p >
</ body >
</ html >
, , , . Controller Action , .
* This source code was highlighted with Source Code Highlighter .
[ Serializable ]
public class Result
{
[ XmlAttribute ]
public string Controller { get ; set ; }
[ XmlAttribute ]
public string Action { get ; set ; }
public List Data { get ; set ; }
}
, , .
Data XmlModel, , XmlInclude XmlModel.
XML- :
< Result Controller ="SomeController" Action ="SomeAction" >
< Data >
<!-- -->
</ Data >
</ Result >
ActionResult. , . c XML-, :
private string _data;
public XmlResult( string data)
{
_data = data;
}
, - ExecuteResult. . :
public override void ExecuteResult(ControllerContext controllerContext)
{
var xslTransform = new XslCompiledTransform();
//
var controller = controllerContext.RouteData.Values[ "controller" ] as string ;
var action = controllerContext.RouteData.Values[ "action" ] as string ;
var templatePath = string .Format( "~/Views/{0}/{1}.xsl" , controller, action);
var virtualFile = HostingEnvironment.VirtualPathProvider.GetFile(templatePath);
// :
//var virtualFile = HostingEnvironment.VirtualPathProvider.GetFile("~/Views/default.xsl");
var xmlReader = XmlReader.Create(virtualFile.Open());
xslTransform.Load(xmlReader);
xmlReader.Close();
var stringReader = new StringReader (_data);
xmlReader = XmlReader.Create(stringReader);
// XML- http-
var xmlWriter = XmlWriter.Create(controllerContext. HttpContext .Response.OutputStream);
xslTransform.Transform(xmlReader, xmlWriter);
stringReader.Close();
xmlReader.Close();
xmlWriter.Close();
}
:
public class XmlResult : ActionResult
{
private string _data;
public XmlResult( string data)
{
_data = data;
}
public override void ExecuteResult(ControllerContext controllerContext)
{
var xslTransform = new XslCompiledTransform();
//
var controller = controllerContext.RouteData.Values[ "controller" ] as string ;
var action = controllerContext.RouteData.Values[ "action" ] as string ;
var templatePath = string .Format( "~/Views/{0}/{1}.xsl" , controller, action);
var virtualFile = HostingEnvironment.VirtualPathProvider.GetFile(templatePath);
// :
//var virtualFile = HostingEnvironment.VirtualPathProvider.GetFile("~/Views/default.xsl");
var xmlReader = XmlReader.Create(virtualFile.Open());
xslTransform.Load(xmlReader);
xmlReader.Close();
var stringReader = new StringReader (_data);
xmlReader = XmlReader.Create(stringReader);
// XML- http-
var xmlWriter = XmlWriter.Create(controllerContext. HttpContext .Response.OutputStream);
xslTransform.Transform(xmlReader, xmlWriter);
stringReader.Close();
xmlReader.Close();
xmlWriter.Close();
}
}
.
ViewData, :
public new List ViewData = new List ();
View, ActionResult . :
public new ActionResult View()
{
//
var result = new Result
{
Controller = ControllerContext.RouteData.Values[ "controller" ] as string ,
Action = ControllerContext.RouteData.Values[ "action" ] as string ,
Data = ViewData
};
var xmlSerializer = new XmlSerializer( typeof (Result));
var stringWriter = new StringWriter ();
xmlSerializer.Serialize(stringWriter, result);
stringWriter.Close();
// ActionResult
return new XmlResult(stringWriter.ToString());
}
:
public abstract class XsltController : Controller
{
public new List ViewData = new List ();
public new ActionResult View()
{
//
var result = new Result
{
Controller = ControllerContext.RouteData.Values[ "controller" ] as string ,
Action = ControllerContext.RouteData.Values[ "action" ] as string ,
Data = ViewData
};
var xmlSerializer = new XmlSerializer( typeof (Result));
var stringWriter = new StringWriter ();
xmlSerializer.Serialize(stringWriter, result);
stringWriter.Close();
// ActionResult
return new XmlResult(stringWriter.ToString());
}
}
:
public class HomeController : XsltController
{
public ActionResult Index()
{
var user = new UserModel { UserId = 1, Login = "root" , Email = "noreply@localhost" };
ViewData.Add(user);
return View();
}
}
. XML:
<? xml version ="1.0" encoding ="utf-16" ? >
< Result xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd ="http://www.w3.org/2001/XMLSchema" Controller ="Home" Action ="Index" >
< Data >
< XmlModel xsi:type ="UserModel" UserId ="1" >
< Login > root </ Login >
< Email > noreply@localhost </ Email >
</ XmlModel >
</ Data >
</ Result >
XSLT-, :
<? xml version ="1.0" encoding ="utf-8" ? >
< xsl:stylesheet version ="1.0" xmlns:xsl ="http://www.w3.org/1999/XSL/Transform" xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" >
< xsl:output encoding ="utf-8"
method ="xml"
media-type ="application/xhtml+xml"
doctype-public ="-//W3C//DTD XHTML 1.1//EN"
doctype-system ="http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd" />
< xsl:template match ="/" >
< html xmlns ="http://www.w3.org/1999/xhtml" >
< head >
< meta http-equiv ="Content-Type" content ="application/xhtml+xml; charset=utf-8" />
< title > XSLT + MVC </ title >
</ head >
< body >
< p > Controller: < xsl:value-of select ="/Result/@Controller" /></ p >
< p > Action: < xsl:value-of select ="/Result/@Action" /></ p >
< p > User: < xsl:value-of select ="//XmlModel[@xsi:type = 'UserModel']/Login" /></ p >
</ body >
</ html >
</ xsl:template >
</ xsl:stylesheet >
HTML-:
<? xml version ="1.0" encoding ="utf-8" ? >
< html xmlns ="http://www.w3.org/1999/xhtml" xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" >
< head >
< meta http-equiv ="Content-Type" content ="application/xhtml+xml; charset=utf-8" />
< title > XSLT + MVC </ title >
</ head >
< body >
< p > Controller: Home </ p >
< p > Action: Index </ p >
< p > User: root </ p >
</ body >
</ html >
, , , . Controller Action , .
* This source code was highlighted with Source Code Highlighter .
[ Serializable ]
public class Result
{
[ XmlAttribute ]
public string Controller { get ; set ; }
[ XmlAttribute ]
public string Action { get ; set ; }
public List Data { get ; set ; }
}
, , .
Data XmlModel, , XmlInclude XmlModel.
XML- :
< Result Controller ="SomeController" Action ="SomeAction" >
< Data >
<!-- -->
</ Data >
</ Result >
ActionResult. , . c XML-, :
private string _data;
public XmlResult( string data)
{
_data = data;
}
, - ExecuteResult. . :
public override void ExecuteResult(ControllerContext controllerContext)
{
var xslTransform = new XslCompiledTransform();
//
var controller = controllerContext.RouteData.Values[ "controller" ] as string ;
var action = controllerContext.RouteData.Values[ "action" ] as string ;
var templatePath = string .Format( "~/Views/{0}/{1}.xsl" , controller, action);
var virtualFile = HostingEnvironment.VirtualPathProvider.GetFile(templatePath);
// :
//var virtualFile = HostingEnvironment.VirtualPathProvider.GetFile("~/Views/default.xsl");
var xmlReader = XmlReader.Create(virtualFile.Open());
xslTransform.Load(xmlReader);
xmlReader.Close();
var stringReader = new StringReader (_data);
xmlReader = XmlReader.Create(stringReader);
// XML- http-
var xmlWriter = XmlWriter.Create(controllerContext. HttpContext .Response.OutputStream);
xslTransform.Transform(xmlReader, xmlWriter);
stringReader.Close();
xmlReader.Close();
xmlWriter.Close();
}
:
public class XmlResult : ActionResult
{
private string _data;
public XmlResult( string data)
{
_data = data;
}
public override void ExecuteResult(ControllerContext controllerContext)
{
var xslTransform = new XslCompiledTransform();
//
var controller = controllerContext.RouteData.Values[ "controller" ] as string ;
var action = controllerContext.RouteData.Values[ "action" ] as string ;
var templatePath = string .Format( "~/Views/{0}/{1}.xsl" , controller, action);
var virtualFile = HostingEnvironment.VirtualPathProvider.GetFile(templatePath);
// :
//var virtualFile = HostingEnvironment.VirtualPathProvider.GetFile("~/Views/default.xsl");
var xmlReader = XmlReader.Create(virtualFile.Open());
xslTransform.Load(xmlReader);
xmlReader.Close();
var stringReader = new StringReader (_data);
xmlReader = XmlReader.Create(stringReader);
// XML- http-
var xmlWriter = XmlWriter.Create(controllerContext. HttpContext .Response.OutputStream);
xslTransform.Transform(xmlReader, xmlWriter);
stringReader.Close();
xmlReader.Close();
xmlWriter.Close();
}
}
.
ViewData, :
public new List ViewData = new List ();
View, ActionResult . :
public new ActionResult View()
{
//
var result = new Result
{
Controller = ControllerContext.RouteData.Values[ "controller" ] as string ,
Action = ControllerContext.RouteData.Values[ "action" ] as string ,
Data = ViewData
};
var xmlSerializer = new XmlSerializer( typeof (Result));
var stringWriter = new StringWriter ();
xmlSerializer.Serialize(stringWriter, result);
stringWriter.Close();
// ActionResult
return new XmlResult(stringWriter.ToString());
}
:
public abstract class XsltController : Controller
{
public new List ViewData = new List ();
public new ActionResult View()
{
//
var result = new Result
{
Controller = ControllerContext.RouteData.Values[ "controller" ] as string ,
Action = ControllerContext.RouteData.Values[ "action" ] as string ,
Data = ViewData
};
var xmlSerializer = new XmlSerializer( typeof (Result));
var stringWriter = new StringWriter ();
xmlSerializer.Serialize(stringWriter, result);
stringWriter.Close();
// ActionResult
return new XmlResult(stringWriter.ToString());
}
}
:
public class HomeController : XsltController
{
public ActionResult Index()
{
var user = new UserModel { UserId = 1, Login = "root" , Email = "noreply@localhost" };
ViewData.Add(user);
return View();
}
}
. XML:
<? xml version ="1.0" encoding ="utf-16" ? >
< Result xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd ="http://www.w3.org/2001/XMLSchema" Controller ="Home" Action ="Index" >
< Data >
< XmlModel xsi:type ="UserModel" UserId ="1" >
< Login > root </ Login >
< Email > noreply@localhost </ Email >
</ XmlModel >
</ Data >
</ Result >
XSLT-, :
<? xml version ="1.0" encoding ="utf-8" ? >
< xsl:stylesheet version ="1.0" xmlns:xsl ="http://www.w3.org/1999/XSL/Transform" xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" >
< xsl:output encoding ="utf-8"
method ="xml"
media-type ="application/xhtml+xml"
doctype-public ="-//W3C//DTD XHTML 1.1//EN"
doctype-system ="http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd" />
< xsl:template match ="/" >
< html xmlns ="http://www.w3.org/1999/xhtml" >
< head >
< meta http-equiv ="Content-Type" content ="application/xhtml+xml; charset=utf-8" />
< title > XSLT + MVC </ title >
</ head >
< body >
< p > Controller: < xsl:value-of select ="/Result/@Controller" /></ p >
< p > Action: < xsl:value-of select ="/Result/@Action" /></ p >
< p > User: < xsl:value-of select ="//XmlModel[@xsi:type = 'UserModel']/Login" /></ p >
</ body >
</ html >
</ xsl:template >
</ xsl:stylesheet >
HTML-:
<? xml version ="1.0" encoding ="utf-8" ? >
< html xmlns ="http://www.w3.org/1999/xhtml" xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" >
< head >
< meta http-equiv ="Content-Type" content ="application/xhtml+xml; charset=utf-8" />
< title > XSLT + MVC </ title >
</ head >
< body >
< p > Controller: Home </ p >
< p > Action: Index </ p >
< p > User: root </ p >
</ body >
</ html >
, , , . Controller Action , .
* This source code was highlighted with Source Code Highlighter .