I couldn’t think of a reasonable title for this post.
The problem I’m having is that I have an SQL database attached to my MVC website. Within the website I have a news/blog system that I have worked on which stores the data in the database and pulls the information on request.
The problem is the routing. I currently have it set up to pull the information about the routing of each individual page as this:
var newsr = new NewsResources();
foreach (var item in newsr.GetAllNewsItems())
{
item.Title = item.Title.Replace(‘ ‘, ‘-‘).ToLower();routes.MapRoute(item.Title, "News/" + item.Title, new {controller = "News", action = "Post", id = item.ID});}
When I add a new news item however, this doesn’t go into the routing system which is proving to be a right pain. I’ve had a google search for dynamically adding url routing but I can’t seem to find a solution.
What I want to know is, is it possible to add a page to the routing system via the page controller once I have saved the post into the database?
I do not think you need to Do a For Each loop across all your POst item and add routes for that. You may do it like this
This will route
../News/SomeTitlerequest to yourPostFromTitleaction method ofNewsController. Read the title there and get the post from there.