[RESOLVED] Linking to page URLS that will change
I am currently working on a project which involves creating many web controls, some of these web controls have buttons and links which post to aspx pages. However these pages are likely to change and could again change in future.
It would be nice if I could list the pages as names in a config file and the resolve page name and do a lookup so when they change I just change the config file rather than all the controls.
Firstly is this possible and/or is there a better method? It would also be good if there was somekind of chaching method so we dont actually have to query the file everytime.
Re: Linking to page URLS that will change
I may be able to answer my own question here, i googled and found http://msdn.microsoft.com/en-us/library/cc668201.aspx Routing, I think this may do what I want but not sure. It seems to require a global asax file which I cannot find in my project also, would this exist and be editable if for example I wanted to use the controls on shaerpoint/umbraco pages?
Re: Linking to page URLS that will change
I now have two tutorials I am trying to mash together to get a solution.
The first
http://erraticdev.blogspot.com/2010/...s-able-to.html
should allow me to create routing without using a global asax file.
HTML Code:
<httpModules>
<add name="MvcRegistratorModule" type="MvcRegistratorModule"/>
</httpModules>
<httpRuntime requestValidationMode="2.0"/>
</system.web>
</configuration>
the second
http://msdn.microsoft.com/en-us/library/ms227673.aspx
should show me how to implement to http module
I added is using System.Web.Routing;
to my module.
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Routing;
/// <summary>
/// Summary description for CustomModules
/// </summary>
public class MvcRegistratorModule : IHttpModule{
/// <summary> /// Disposes of the resources (other than memory) used by the module that implements <see cref="T:System.Web.IHttpModule"/>. /// </summary>
public void Dispose()
{
// dispose any resources if needed
}
/// <summary>
/// Inits the specified application.
/// </summary>
/// <param name="application">The application.</param>
public void Init(HttpApplication application)
{
AreaRegistration.RegisterAllAreas();
this.RegisterRoutes(RouteTable.Routes);
}
/// <summary>
/// Registers application routes.
/// </summary>
/// <param name="routes">Routes collection to which we'd like to assign new routes.</param>
private void RegisterRoutes(RouteCollection routes)
{
// clear routes first just in case this method gets called multiple times during web application's lifetime
routes.Clear();
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
// default application route
routes.MapRoute("Default","{controller}/{action}/{identifier}",
new { controller = "Home", action = "Index", identifier = UrlParameter.Optional });
}
}
Problems - The first seems to be for MVC rather than a normal website. I dont know if that is why this errors in my project but I get an error on AreaRegistration, IgnoreRoute, MapRoute not being in the current context or definition.
Re: Linking to page URLS that will change
Changed to
Code:
private void RegisterRoutes(RouteCollection routes)
{
// clear routes first just in case this method gets called multiple times during web application's lifetime
routes.Clear();
// default application route
routes.MapPageRoute("", "TestPage", "~/TestPage.aspx");
}
for simplicity,
Re: Linking to page URLS that will change
Hello,
What version of ASP.Net are you using? The routing that is available in ASP.Net MVC is now available in ASP.Net 4.0, so if you have the choice, you can use that. Otherwise, you are going to have to use something like UrlRewrite to do this work for you.
Gary
Re: Linking to page URLS that will change
I have simple routing working in .net 4 and it works well.
<asp:HyperLink ID="HyperLinkHelp" runat="server"
NavigateUrl="<%$RouteUrl:RouteName=Help, Topic=1%>" Text="Help" ImageUrl="/images/help_icon_transparent.png"/>
Im just wondering how in the example above I would pragmatically specify the value for Topic.
i.e. on the fly change it from 1 to 2?
Re: Linking to page URLS that will change
Code:
set { HelpID = value;
RouteValueDictionary parameters =
new RouteValueDictionary
{
{"Topic", HelpID }
};
VirtualPathData vpd =
RouteTable.Routes.GetVirtualPath(null, "GSPHelp", parameters);
this.HyperLinkHelp.NavigateUrl = vpd.VirtualPath;
}