如何在mvc2.0中创建动态视图页面?

时间:2010-03-23 07:15:17

标签: asp.net-mvc

我试图在运行时创建视图页面意味着,当用户在运行时在文本框中键入一些文本时,应该创建具有该名称的视图页面。

2 个答案:

答案 0 :(得分:2)

可能是我的代码可以帮助您解决此问题

ViewPage的控制器和后代:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Web.UI;
using System.Web.UI.WebControls;
using Site.Models;
using System.Text.RegularExpressions;
using System.Web.Mvc.Html;

namespace Site.Controllers
{

public class MPSViewPage : ViewPage
{
    // Here master page is being seted
    protected override void OnPreInit(EventArgs e)
    {
        Random rnd = new Random();
        int i = rnd.Next(0, 20);
        string masterPageName = (i % 2) == 0 ? "Admin.Master" : "Main.Master";
        string pathMasterPageFile = "~/Views/Shared/" + masterPageName;

        MasterPageFile = pathMasterPageFile;

        base.OnPreInit(e);
    }

    protected override void OnInitComplete(EventArgs e)
    {

        //List of ContentPlaceHolder's id is being got. See later
        MasterPageAnalizer analizer = new MasterPageAnalizer();
        IList<string> contentHolders = analizer.GetBodyPlaceholders(Regex.Match(MasterPageFile, "[^/]*$").ToString());

        //Add content to ContentPlaceHolder
        foreach (string holder in contentHolders)
        {
            ContentPlaceHolder placeHolder = (ContentPlaceHolder)Master.FindControl(holder);
            if (placeHolder != null)
            {
                Content content = new Content();
                placeHolder.Controls.Add(content);
                //Set function for render each content
                content.SetRenderMethodDelegate(RenderIndexDeletegate);
            }
        }


        base.OnInitComplete(e);
    }

    protected void RenderIndexDeletegate(HtmlTextWriter w, Control c)
    {
        //You can use any html helpers for rendering content
        w.Write("Render to <b>" + ((Content)c).Parent.ID + 
            "</b> url: " + Request.Params["URL"] + 
            " with query parameter " + ViewData["parameters"] + " <br />" +
            Html.Action("GetHtmlStatic", "HtmlStatic", new{area = "HtmlStatic"}));
    }

}

public class IndexController : Controller
{
    public ActionResult Index(string parameters)
    {
        ViewData["parameters"] = parameters;
        return View();
    }

}
}

母版分析器:

using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text.RegularExpressions;

namespace Site.Models
{
public class MasterPageAnalizer
{
    private DirectoryInfo dirInfo = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory + "Views\\Shared\\");

    public IList<string> MasterPages{
        get
        {
            IList<String> masterPageNames = new List<string>();
            FileInfo[] files = dirInfo.GetFiles("*.Master");
            foreach (FileInfo file in files)
            {
                masterPageNames.Add(file.Name);
            }
            return masterPageNames;
        }
    }

    public IList<string> GetBodyPlaceholders(string masterPageName)
    {
        IList<string> placeholders = new List<string>();
        string masterPagePath = dirInfo + masterPageName;

        if (File.Exists(masterPagePath))
        {
            string masterPageContent = File.ReadAllText(masterPagePath);
            Regex expression = new Regex(@"<asp:ContentPlaceHolder.*?ID=""(?<placeHolderId>\w+)"".*?>");
            masterPageContent = Regex.Match(masterPageContent, "<body>(.|\n)*</body>",RegexOptions.Multiline).ToString();
            MatchCollection matches = expression.Matches(masterPageContent);
            foreach (Match match in matches)
            {
                placeholders.Add(match.Groups["placeHolderId"].Value);
            }

        }

        return placeholders;
    }
}
}

简单视图:

<%@ Page Title="" Language="C#" Inherits="Site.Controllers.MPSViewPage" %>

祝你好运。

答案 1 :(得分:1)

一切皆有可能。但是,如果您要创建像CMS这样的项目,那么这是不正确的方法。您必须将页面的信息(例如标题,描述等)存储在数据存储中。因此,您只有一个页面。