2

As I just recently changed from WebPages to MVC, and started taking it into practical use, I stumbled across an issue which I haven't been able to find an answer to.

In my administration panel I'm trying to make users able to add dynamic pages, e.g.

"Website.com/Pages/About" - "About" being the dynamic part, the URL should be generated from the database. In WebPages, you could easily do a foreach in the CSHTML and accomplish this, and then in the "Pages"-file, you could grab the dynamic page's URL/name by using "UrlData[0]", however, it seems like I can't use this function in the controller in my MVC project, so I'm looking for an alternative solution for this, I'd like to avoid using ID's in my URL, and simply only have the page's name. I also know I could use query-strings for this, but again, I believe the URL would look a lot better without query-strings in it, at least for this dynamic page-system.

Thank you in advance!

Mikkel
  • 1,853
  • 1
  • 15
  • 31
  • You could use "About" as the id: a route of "Pages/{id}" with a default action on the `PageController` which takes a string id and uses that to lookup the content to be rendered by the view. (There is no requirement ids are integers: it is just very common in examples). – Richard Nov 11 '14 at 15:22
  • Does your user upload an html page? Then when visiting /Pages/About, the uploaded page must show? – AbdulG Nov 11 '14 at 15:42
  • @AbdulG - No, simply edit the text using a WYSIWYG-editor. – Mikkel Nov 11 '14 at 16:07

1 Answers1

1

I think what you want is to configure the routing in RouteConfig.cs.

See: http://www.codeproject.com/Articles/641783/Customizing-Routes-in-ASP-NET-MVC for a tutorial on how to do this.

trees_are_great
  • 3,881
  • 3
  • 31
  • 62
  • I'm going to take a look at the tutorial, I do believe this will sort my issue. - I'll mark the answer if this does the trick for me, thank you! – Mikkel Nov 11 '14 at 15:52
  • My pleasure. Let me know if you have any questions. – trees_are_great Nov 11 '14 at 16:38
  • Right, so I got it working now, except it does it in a query string, and I need it like "website.com/Pages/About" and not "website.com/Pages?pageUrl=About" I'm sorry for the hassle, heh – Mikkel Nov 11 '14 at 17:37
  • Is this coming after using an ActionLink? In which case: http://stackoverflow.com/questions/18734540/mvc-my-url-is-creating-length-4 Otherwise, what have you put in your RouteConfig.cs? – trees_are_great Nov 11 '14 at 17:48
  • It was coming after using the ActionLink, and I've solved that problem, it now doesn't do ?Length=4 anymore, however, now it just doesn't add the page url after it for some reason. Once again, thanks a lot for helping out! – Mikkel Nov 11 '14 at 18:47
  • In my maproute I've put in: routes.MapRoute( name: "Pages", url: "Pages/{pageUrl}", defaults: new { controller = "Home", action = "Pages", pageUrl = UrlParameter.Optional }, namespaces: new[] { "ElevForum.Controllers" } ); – Mikkel Nov 11 '14 at 22:18
  • No problem. I just tried your route on my project (without the namespace argument). It works fine - http://localhost:1138/Pages/PageURL redirects me to the pages action on the home controller. Perhaps the problem is with your actionLink. This worked as an ActionLink for me: @Html.ActionLink("My link Text", "PageURL", "Pages") – trees_are_great Nov 12 '14 at 09:27
  • Thank you very much, it turns out the ActionLink was the problem, I somehow put the route-values under the HtmlAttributes, silly me! Thank you! – Mikkel Nov 12 '14 at 09:58