Reputation: 11
I am trying to create view page at run time means, when the user type some text in textbox at run time then the view page with that name should get created.
Upvotes: 1
Views: 2575
Reputation: 21
May be my code can help you with this
Controller and descendant of 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();
}
}
}
Master page 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;
}
}
}
Simple view:
<%@ Page Title="" Language="C#" Inherits="Site.Controllers.MPSViewPage" %>
Good Luck.
Upvotes: 2
Reputation: 7941
Everything is possible. But if you are after to create a project like CMS, it's not right approach. You have to store the pages' information (such as title, description and etc.) in a data store. Thus, you have merely a single page.
Upvotes: 1