Asp.net mvc 如何在mvc2.0中创建动态视图页面?
我试图在运行时创建视图页面,这意味着,当用户在运行时在文本框中键入一些文本时,应该创建具有该名称的视图页面。一切都是可能的。但如果你想创建一个像CMS这样的项目,这不是正确的方法。您必须将页面信息(如标题、说明等)存储在数据存储中。因此,您只有一个页面。可能是我的代码可以帮助您实现这一点 ViewPage的控制器和子体:Asp.net mvc 如何在mvc2.0中创建动态视图页面?,asp.net-mvc,Asp.net Mvc,我试图在运行时创建视图页面,这意味着,当用户在运行时在文本框中键入一些文本时,应该创建具有该名称的视图页面。一切都是可能的。但如果你想创建一个像CMS这样的项目,这不是正确的方法。您必须将页面信息(如标题、说明等)存储在数据存储中。因此,您只有一个页面。可能是我的代码可以帮助您实现这一点 ViewPage的控制器和子体: using System; using System.Collections.Generic; using System.IO; using System.Linq; usin
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();
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.IO;
使用System.Linq;
使用系统文本;
使用System.Web;
使用System.Web.Mvc;
使用System.Web.UI;
使用System.Web.UI.WebControl;
使用站点模型;
使用System.Text.RegularExpressions;
使用System.Web.Mvc.Html;
名称空间站点.控制器
{
公共类MPSViewPage:查看页面
{
//正在设置母版页
受保护的重写void OnPreInit(EventArgs e)
{
随机rnd=新随机();
int i=rnd.Next(0,20);
字符串masterPageName=(i%2)==0?“Admin.Master”:“Main.Master”;
字符串pathMasterPageFile=“~/Views/Shared/”+masterPageName;
MasterPageFile=路径MasterPageFile;
基于preinit(e);
}
受保护的覆盖无效OnInitComplete(事件参数e)
{
//正在获取ContentPlaceHolder的id列表。请参阅下文
MasterPageAnalizer analizer=新的MasterPageAnalizer();
IList contentHolders=analizer.GetBodyPlaceholders(Regex.Match(MasterPageFile,[^/]*$”).ToString();
//将内容添加到ContentPlaceHolder
foreach(contentHolders中的字符串持有者)
{
ContentPlaceHolder=(ContentPlaceHolder)Master.FindControl(holder);
if(占位符!=null)
{
内容=新内容();
占位符.控件.添加(内容);
//设置渲染每个内容的函数
setRenderMethodElegate(RenderIndexDeletegate);
}
}
基础.完成(e);
}
受保护的无效RenderIndexDeletegate(HtmlTextWriter w,控件c)
{
//您可以使用任何html帮助程序来呈现内容
w、 写入(“呈现到”+((内容)c).Parent.ID+
“url:”+请求.参数[“url”]+
使用查询参数“+ViewData[“parameters”]+”
+
Action(“GetHtmlStatic”,“HtmlStatic”,new{area=“HtmlStatic”});
}
}
公共类IndexController:控制器
{
公共操作结果索引(字符串参数)
{
ViewData[“参数”]=参数;
返回视图();
}
}
}
母版页分析程序:
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;
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.IO;
使用System.Text.RegularExpressions;
名称空间站点.Models
{
公共类主页面分析器
{
private DirectoryInfo dirInfo=new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory+“Views\\Shared\”;
公共IList母版页{
得到
{
IList masterPageNames=新列表();
FileInfo[]files=dirInfo.GetFiles(“*.Master”);
foreach(文件中的文件信息文件)
{
masterPageNames.Add(file.Name);
}
返回主页名;
}
}
公共IList GetBody占位符(字符串masterPageName)
{
IList占位符=新列表();
字符串masterPagePath=dirInfo+masterPageName;
if(File.Exists(masterPagePath))
{
字符串masterPageContent=File.ReadAllText(masterPagePath);
正则表达式=新正则表达式(@“”);
masterPageContent=Regex.Match(masterPageContent,(.|\n)*”,RegexOptions.Multiline.ToString();
MatchCollection matches=表达式.matches(masterPageContent);
foreach(匹配中的匹配)
{
占位符.Add(match.Groups[“占位符ID”].Value);
}
}
返回占位符;
}
}
}
简单视图:
<%@ Page Title="" Language="C#" Inherits="Site.Controllers.MPSViewPage" %>
祝你好运。此查看页面的内容将从何而来?