david
david

Reputation: 203

Dynamic Routes from sql database

I am quite new to ASP.NET Core and routing and I am looking for an advice. In the old days I would have a page like CMS or page.aspx.

My main question is how would I go about creating a unique url off the route, i.e. www.mydomain.com/test-page or if they pick a sub category www.mydomain.com/cat/page.

I know it kinda has to do with routes, but I do not know how one makes them dynamic. Any advice be appreciated. I am using ASP.NET Core 1.2.

Also I want to be able to give the end user ability to pick from certain views, i.e. kinda of like how you could have done with master pages.

Edit Correction Hi folks sorry for confusion this is asp.net core app 1.1 not version 2 as pointed out to me below

I have a controller called CMS controller

private solitudeDContext _context;

    public CmsController(solitudeDContext context)
    {
        _context = context;
    }


    public IActionResult GetContent(string slug)
    {

        CmsPages _page = new CmsPages();

        _page= _context.Pages.Where(w=>w.slug==slug).Select(s=>s.Content).Single();


    }
}

Class for CMS pages

public  class CmsPages
{
    [Key]
    public int id { get; set; }

    public string Title { get; set; }

    public string Description { get; set; }

     public string slug { get; set; }

    public string Content { get; set; }

    public string PageView { get; set; }

    public DateTime startDate { get; set; }

    public DateTime EndDate { get; set; }

    public bool isDeleted { get; set; }

    public bool isVisible { get; set; }



    public string Creator { get; set; }

    public PageStatus PageStatuses { get; set; }

}

Edit 2

Ok so i tried the following

    private solitudeDContext _context;

    public CmsController(solitudeDContext context)
    {
        _context = context;
    }


    public IActionResult GetContent(string slug)
    {

        CmsPages _page = new CmsPages();

       var  _content= _context.Pages.Where(w=>w.slug==slug).Select(s=>s.Content).Single();

        return View();

    }
    public IActionResult Index()
    {

        return View();
    }

And in my view its just showing a blank page am i missing something must I have a view as well cms for this ?.

        app.UseStaticFiles();
        app.UseIdentity();
        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");


            routes.MapRoute(
              name: "cmsroute",
              template: "{slug}"
              , defaults: new { controller = "Cms", action = "GetContent" }
              );

Upvotes: 0

Views: 1583

Answers (1)

Joe Audette
Joe Audette

Reputation: 36716

You can just add a route something like this in Startup.cs

routes.MapRoute(
           name: "cmsroute",
           template: "{slug}"
           , defaults: new { controller = "Cms", action = "GetContent" }
           );

Note also rather than build your own new cms you might find my project cloudscribe.SimpleContent is useful to you, even if you do want to build your own you can study the code and get ideas on how to do things

Upvotes: 3

Related Questions