locked
urlMappings: Dynamic URL Mapping? possible to get from a database? RRS feed

  • Question

  • User-612023724 posted
    I am working on a e-commerce site, using c# and asp.net 2.0.

    I would like the client to be able to add or delete items to this online store

    Im pretty sure I can figure that part out.... but I would also like them to use "friendly urls" to hopefully reduce the chances of being on the receiving end of a CSS (cross site scripting) hack - and also will be better for search engines and will just look nicer.

    I have discovered the <urlMappings> tag that you can put into web.config which can create friendly urls.... but how do I get <urlMappings> to get its information from the database of products?

    All I know so far is how to code them in myself, eg:

    <urlMappings enabled="true">
    <add
    url="~/Category.aspx"
    mappedUrl="~/Default.aspx?category=default" />
    <add
    url="~/Autos.aspx"
    mappedUrl="~/Default.aspx?category=autos" />
    <add
    url="~/Games.aspx"
    mappedUrl="~/Default.aspx?category=games" />
    <add
    url="~/Health.aspx"
    mappedUrl="~/Default.aspx?category=health" />
    <add
    url="~/News.aspx"
    mappedUrl="~/Default.aspx?category=news" />
    </urlMappings>

    and that wouldnt be any good for a site that will have products added or removed on the fly.
    Friday, June 16, 2006 10:36 AM

All replies

  • User-2118745002 posted

    You can add items to the urlMappings collection using the configuration API:

            Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
            UrlMappingsSection section = (UrlMappingsSection)config.GetSection("system.web/urlMappings");
            section.UrlMappings.Add(new UrlMapping("~/Autos.aspx", "~/repro1.aspx?category=Autos"));
            config.Save();

    You can iterate throu your query, adding items to the collection.

    But note that changing the web.config of an application will unload the app domain. This can hit your app performance.

    Friday, June 16, 2006 8:17 PM
  • User-612023724 posted
    get in! it works :) cheers for the help dude :D I can use this code for when a new product/page is added :)
    Monday, June 19, 2006 5:25 AM
  • User-1058960925 posted

    You would do better to implement the interface IHttpModule in which you can do any database lookups you want without having to hack your config each time you change your products list, or having a massive config file. You then reference this in the config file at <system.web> <httpModules> <add name="MyHttpModule" type="MyNamespace.MyHttpModule"/>.

    If you try this let me know if you get it working.

    Friday, April 3, 2009 4:44 PM
  • User2000895651 posted

    Hello  josere,

    I have Used this solution at local end it's working fine. But on Server this through an exception "An error occurred loading a configuration file: Access to the path 'C:\inetpub\wwwroot\StepCare\jweyxdcw.tmp' is denied.".

    Please suggest regarding this,

    Thanks,

    Gorav

    Tuesday, December 1, 2015 6:11 AM