2

The problem for me is to understand how to render the dynamically created pages with a uniq URL's when the page don't have a Controller/View to map to.

I am building a CMS-system In ASP.NET MVC 3 3 with Razor. In a database I store the page/site structure and content.

I guess i need to have some render action in a controller that create a custom view with the content from database? and what about the URL's?

0

1 Answer 1

3

I'd make a separate folder (like "DynamicContent" or something) to keep those dynamic pages in and add corresponding IgnoreRoute call to RegisterRoutes method in Global.asax.cs, like this:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("DynamicContent/{*pathInfo}");

    ...
}

After that users will be able to access those pages using URLs like

http://%your_site%/DynamicContent/%path_to_specific_file%

UPDATE

If you don't want to lay files on server HDD then you may really create a special controller for those files. Route for this should look this:

public static void RegisterRoutes(RouteCollection routes)
{
    ...

    routes.MapRoute(
        "DynamicRoute", // Route name
        "Dynamic/{*pathInfo}", // URL with parameters
        new { controller = "Dynamic", action = "Index"} // Parameter defaults
    );

}

Your DynamicController.cs should look like this:

public class DynamicController : Controller
{
    public ActionResult Index(string pathInfo)
    {
        // use pathInfo value to get content from DB
        ...
        // then 
        return new ContentResult { Content = "%HTML/JS/Anything content from database as string here%", ContentType = "%Content type either from database or inferred from file extension%"}
        // or (for images, document files etc.)
        return new FileContentResult(%file content from DB as byte[]%, "%filename to show to client user%");
    }
}

Note that asterisk (*) before pathInfo will make this route grab entire URL part after Dynamic - so if you have entered http://%your_site%/Dynamic/path/to/file/something.html then an entire string path/to/file/something.html will be passed in parameter pathInfo to DynamicController/Index method.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for answer, but if i what the URL's to mirror the dynamically created structure in the database. ex: www.mysite.com/about/contact then the about and contact is just a page-structure in the database. I hope you understand what i mean ? :)
@Mickjohansson: Modified my answer accordingly, hope it helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.