Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Create a CMS Application Using Asp.Net MVC C#

I had reviewed lots of questions already posted on StackOverFlow and most of the answers ended up on what CMS platform to use instead of how to actually create one from Scratch.

Searching the Internet and Books also did not result in any useful answers to my questions.

I am fairly new to using Asp.Net MVC and wanted to know how to make a very basic CMS with Asp.Net, MVC, Entity Framework, in C#.

My main issue is creating database driven pages that are not hard coded into the MVC Controller and not physically located as Somepage.cshtml

Would like to know how to do the following:

Incoming URL: MainWebsite.com/DatabaseProvidedPage

Goal is for the controller in Route for pages without a /controller/ in the URL to default as being a Database Driven/Created Webpage.

Would like to actually know how to do this.

Not looking for a platform to use to do this.

I want to learn the basics of creating a CMS using Asp.Net MVC.

Just really want:

How to do this?

URL: MainWebsite/DatabaseProvidedWebpage --> Return to user the HTML stored in database

Thanks,

like image 337
Kbdavis07 Avatar asked Sep 03 '25 04:09

Kbdavis07


2 Answers

Found Solution:

Routing

// Display Database Provided Page MainSite.Com/{PageName}

routes.MapRoute("DatabasePage", 
"{PageName}", 
new { controller = "Home", 
action = "Index", 
PageName = "" 
});

Controller:

public ActionResult Index(String PageName)
        {
            if (PageName == null)
                {
                    List<PageTable> pages = PagesRepository.GetAllPages();
                    return View(pages);
                }

            PageTable FindPageName = PagesRepository.GetPageByURL(PageName);



            if (FindPageName == null)
                {
                    return Content(PageName);
                }


            if (FindPageName != null)
                {
                    return View(FindPageName);
                }

            return View("");

        }

View

@model CMS_Repository.DAL.EF.PageTable

@{
ViewBag.Title = "Details";
}

<h2>Details</h2>

    <div>
    <h4>PageTable</h4>
    <hr />
    <dl class="dl-horizontal">
        <dt>
            @Html.DisplayNameFor(model => model.Title)
            </dt>

        <dd>
            @Html.DisplayFor(model => model.Title)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.Content)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.Content)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.FriendlyURL)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.FriendlyURL)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.ControllerName)
        </dt>


</dl>

like image 175
Kbdavis07 Avatar answered Sep 05 '25 21:09

Kbdavis07


Scaffolding does this automatically for you, only not with Guids. When you scaffold, you get a controller with several actions, one of which is Details. By accessing /Controller/Details/[id] you see details about the specified item. Create a table with a column for your HTML; let's call it HTML. In the Details view, write

@Html.Raw(Model.HTML)
like image 41
andypopa Avatar answered Sep 05 '25 20:09

andypopa