ASP.NET MVC рдореЗрдВ XSLT рдЯреЗрдореНрдкрд▓реЗрдЯ рдЗрдВрдЬрди рдХрд╛ рдЙрдкрдпреЛрдЧ рдХрд░рдирд╛

рдЕрдм рд╡рд┐рдХрд╛рд╕ рдореЗрдВ, рдПрдорд╡реАрд╕реА рдЯреЗрдореНрдкрд▓реЗрдЯ рдХрд╛ рд╡реНрдпрд╛рдкрдХ рд░реВрдк рд╕реЗ рдЙрдкрдпреЛрдЧ рдХрд┐рдпрд╛ рдЬрд╛рддрд╛ рд╣реИ, рдЬреЛ рдбреЗрдЯрд╛ рдореЙрдбрд▓, рдкреНрд░рд╕реНрддреБрддрд┐ рдФрд░ рдХрд╛рд░реНрдп рдХреЗ рддрд░реНрдХ рдХреЛ рдЕрд▓рдЧ рдХрд░рддрд╛ рд╣реИред рд╡реЗрдм рд╡рд┐рдХрд╛рд╕ рдореЗрдВ рдЗрд╕ рдЯреЗрдореНрдкрд▓реЗрдЯ рдХрд╛ рдПрдХ рдХрд╛рдлреА рдкреНрд░рд╕рд┐рджреНрдз рдХрд╛рд░реНрдпрд╛рдиреНрд╡рдпрди ASP.NET MVC рдлреНрд░реЗрдорд╡рд░реНрдХ рд╣реИред рдмрд╣реБрдд рд╕рдордп рдкрд╣рд▓реЗ рдирд╣реАрдВ, рдЗрд╕ рдврд╛рдВрдЪреЗ рдХрд╛ рддреАрд╕рд░рд╛ рд╕рдВрд╕реНрдХрд░рдг рдЬрд╛рд░реА рдХрд┐рдпрд╛ рдЧрдпрд╛ рдерд╛, рдЬрд┐рд╕рдореЗрдВ рдкреНрд░рддрд┐рдирд┐рдзрд┐рддреНрд╡ рдХреЛ рд▓рд╛рдЧреВ рдХрд░рдиреЗ рдХреЗ рд▓рд┐рдП рджреЛ рд╡рд┐рдХрд▓реНрдк рд╣реИрдВ: ASPX рдФрд░ рд░реЗрдЬрд░ред рджреЛрдиреЛрдВ рдЗрдВрдЬрдиреЛрдВ рдореЗрдВ рд╕реБрд╡рд┐рдзрд╛рдУрдВ рдХреА рдПрдХ рд╡рд┐рд╕реНрддреГрдд рд╢реНрд░реГрдВрдЦрд▓рд╛ рд╣реИ, рд▓реЗрдХрд┐рди рдЗрд╕ рдкреНрд░рдХрд╛рд╢рди рдореЗрдВ рдореИрдВ рдкреНрд░рддрд┐рдирд┐рдзрд┐рддреНрд╡ рдХреЛ рд▓рд╛рдЧреВ рдХрд░рдиреЗ рдХреЗ рд▓рд┐рдП рдПрдХ рдФрд░ рдЙрдкрдХрд░рдг рдХреЗ рдЙрдкрдпреЛрдЧ рдХрд╛ рд╡рд░реНрдгрди рдХрд░рдиреЗ рдХреА рдХреЛрд╢рд┐рд╢ рдХрд░реВрдВрдЧрд╛ - XSL рдХреЗ рдЯреЗрдореНрдкрд▓реЗрдЯред



рдПрдХ XSLT рдЯреЗрдореНрдкрд▓реЗрдЯ рдПрдХ XML рджрд╕реНрддрд╛рд╡реЗрдЬрд╝ рд╣реИ рдЬреЛ рдЖрдЙрдЯрдкреБрдЯ рдореЗрдВ рдПрдХ рдпрд╛ рдЕрдзрд┐рдХ рдЗрдирдкреБрдЯ XML рджрд╕реНрддрд╛рд╡реЗрдЬрд╝реЛрдВ рдХреЗ рд░реВрдкрд╛рдВрддрд░рдг рдХрд╛ рд╡рд░реНрдгрди рдХрд░рддрд╛ рд╣реИ, рдЬреЛ рдЖрдорддреМрд░ рдкрд░ XML рдпрд╛ HTML рджрд╕реНрддрд╛рд╡реЗрдЬрд╝ рднреА рд╣реЛрддрд╛ рд╣реИред рд╣рдорд╛рд░реЗ рдорд╛рдорд▓реЗ рдореЗрдВ, рдЯреЗрдореНрдкрд▓реЗрдЯ рдХреЗ рд▓рд┐рдП рдЗрдирдкреБрдЯ рд╕рдВрдмрдВрдзрд┐рдд рдореЙрдбрд▓ рд╕реЗ рдкреНрд░рд╛рдкреНрдд рдбреЗрдЯрд╛ рдХрд╛ рдПрдХреНрд╕рдПрдордПрд▓ рдкреНрд░рддрд┐рдирд┐рдзрд┐рддреНрд╡ рд╣реЛрдЧрд╛ред рдкреГрд╖реНрда рд╕рд╛рдордЧреНрд░реА рдХреЛ рдЬреЗрдирд░реЗрдЯ рдХрд░рдиреЗ рдХреЗ рд▓рд┐рдП рдЯреЗрдореНрдкреНрд▓реЗрдЯ рдореЗрдВ рд╕реНрдерд╛рдирд╛рдВрддрд░рд┐рдд рдбреЗрдЯрд╛ рдХрд╛ рдЙрдкрдпреЛрдЧ рдХрд┐рдпрд╛ рдЬрд╛рддрд╛ рд╣реИред

рд╣рдорд╛рд░рд╛ рдореБрдЦреНрдп рдкреНрд░реЛрдЬреЗрдХреНрдЯ ASP.NET MVC 3 рдПрдкреНрд▓реАрдХреЗрд╢рди рдЯреЗрдореНрдкреНрд▓реЗрдЯ рд╣реЛрдЧрд╛ред рдЪреБрдирд┐рдВрджрд╛ рд╡реНрдпреВ рдЗрдВрдЬрди рдпрд╣рд╛рдВ рдХреЛрдИ рдорд╛рдпрдиреЗ рдирд╣реАрдВ рд░рдЦрддрд╛ред



рдбреЗрдЯрд╛ рдореЙрдбрд▓



рдкреНрд░рддрд┐рдирд┐рдзрд┐рддреНрд╡ рдХреЗ рд▓рд┐рдП рдкрд╛рд░рд┐рдд рд╕рднреА рдореЙрдбрд▓реЛрдВ рдХреЗ рд▓рд┐рдП, рд╣рдо рдирд┐рдореНрдирд▓рд┐рдЦрд┐рдд рдЖрдзрд╛рд░ рд╡рд░реНрдЧ рдХрд╛ рд╡рд░реНрдгрди рдХрд░рддреЗ рд╣реИрдВ:



[ Serializable ]

[XmlInclude( typeof (UserModel))]

public abstract class XmlModel { }








рдкрд╣рд▓реА рд╡рд┐рд╢реЗрд╖рддрд╛ рдХреА рдЖрд╡рд╢реНрдпрдХрддрд╛ рд╣реИ рдХреНрдпреЛрдВрдХрд┐ рдЗрд╕ рд╡рд░реНрдЧ рдХреА рд╡рд╕реНрддреБрдУрдВ рдХреЛ рдПрдХреНрд╕рдПрдордПрд▓ рдореЗрдВ рдХреНрд░рдордмрджреНрдз рдХрд┐рдпрд╛ рдЬрд╛рдПрдЧрд╛ред рджреВрд╕рд░реА рд╡рд┐рд╢реЗрд╖рддрд╛ рдХрд╛ рдЙрдкрдпреЛрдЧ рдХрд░рдиреЗ рдХреЗ рдХрд╛рд░рдгреЛрдВ рдкрд░ рдиреАрдЪреЗ рдЪрд░реНрдЪрд╛ рдХреА рдЬрд╛рдПрдЧреАред

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 .















All Articles