2010-03-23 64 views
1

我试图在运行时创建视图页的方式,当用户在运行时在文本框中键入一些文本时,应该创建具有该名称的视图页。如何在mvc2.0中创建动态视图页面?

+1

这个视图页的内容将来自哪里? – 2010-03-23 07:23:14

回答

1

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

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(); 
    } 

} 
} 

母版页analizer:

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" %> 

好路CK。