I'm implementing a new site in MVC 5, and we're combining old content into one long, flowing page with fragment links (#ChildAction
). I've opted to use Controller.Index
as the primary page, which then has content, and then calls the child actions (with [ChildActionOnly]
attribute.
My menus are generated by my own custom code for now (I couldn't find a way to make MvcSitemapProvider do what I want, and couldn't find anything else), but I'd like to somehow be able to generate my actions url as a fragment using Url.Action
, like Controller#Action
, instead of Controller/Action
. What's the best way to achieve? Bonus points for coming up with a menu solution as well.
Should I just stick to writing my own Url.MyAction
extension method, with my own custom Menus?
Since fragments are client-side only, the only way you could potentially make this work is to make your child action calls from JavaScript. Browsers do not send fragment information to the server when requesting URLs, so there would be no other way for the application to understand which action you are requesting.
That said, if you did use MvcSiteMapProvider, you could use a custom attribute to configure your fragment data and modify the
/Views/Shared/DisplayTemplates/SiteMapNodeModel.cshtml
to output the fragment at the end of the URL. See this answer for an example.