Understanding asp.net mvc IView and IView.Render

I am trying to develop a method for executing VERY simple asp.net mvc plugins, but basically I'm trying to understand how view rendering works.

I redid my problem before this

public class CustomView : IView
{
    public void Render(ViewContext viewContext, TextWriter writer)
    {
        writer.Write( /* string to render */);
    }
}

Now, as part of this recording method, I can display any line in the view, but when I put a line of code wrapped in <%%> there, it renders the code in the view literally, rather than parsing it and executing it. I tried adding things like <% @Page ... to the beginning of the line, and that just does it literally as well.

Among the many attempts that I now call this way as part of the controller action:

...
CustomView customView = new CustomView();

ViewResult result = new ViewResult();
result.View = customView;
result.ViewName = "Index.aspx";
result.MasterName = "";
return result;

What am I missing or is something wrong that this will not work? It seems that ViewResult has a WebFormViewEngine in its ViewEngines collection.

, , , , , , . , - /, , .

+3
2

<%% > ASP.NET WebForms. WebForms ( ASP.NET, MVC-) , , ASP.NET ( , Response.Write, ..)

, , , .

, , - .aspx -, .

, ...

+1

Microsoft , ; :

  • IViewEngine XSLT
  • IView

:

http://www.codeproject.com/Articles/426768/Implementing-an-XSLT-View-Engine-for-ASP-NET-MVC

XSLT :

public class XsltViewEngine : VirtualPathProviderViewEngine
{
    public XsltViewEngine()
    {
        ViewLocationFormats = new[] 
            { 
                "~/XSLTs/{1}/{0}.xsl", "~/XSLTs/Shared/{0}.xsl", 
                "~/XSLTs/{1}/{0}.xslt", "~/XSLTs/Shared/{0}.xslt" 
            };
        PartialViewLocationFormats = ViewLocationFormats;
    }

    protected override IView CreatePartialView(ControllerContext controllerContext, string partialPath)
    {
        return new XsltView(partialPath);
    }

    protected override IView CreateView(ControllerContext controllerContext, string viewPath, string masterPath)
    {
        return new XsltView(viewPath);
    }
}

:

public class XsltView : IView
{
    private readonly string _path;

    public XsltView(string path)
    {
        _path = path;
    }

    public void Render(ViewContext viewContext, TextWriter writer)
    {
        var xsltFile = viewContext.HttpContext.Server.MapPath(_path);
        var xmlData = viewContext.ViewData["data"] != null 
            ? ((XElement)viewContext.ViewData["data"]).ToString() 
            : "";

        var xmlTree = XDocument.Parse(xmlData);
        var xslt = new XslCompiledTransform();

        xslt.Load(xsltFile);
        xslt.Transform(xmlTree.CreateReader(), null, writer);
    }
}

, Global.asax:

protected void Application_Start()
{
    ViewEngines.Engines.Add(new XsltViewEngine());

    AreaRegistration.RegisterAllAreas();

    RegisterGlobalFilters(GlobalFilters.Filters);
    RegisterRoutes(RouteTable.Routes);
}
-1

Source: https://habr.com/ru/post/1750424/


All Articles