Search code examples
c#asp.netnavigateurl

Asp.net - Remove Folder Structure From My URL


Appologise if this has already been asked but I have an asp.net website and all my footer pages are stored in Visual Studio under

Views > Footer > [Page Names]

When i click on a footer link, my URL is displaying as:

http://www.mysite.co.uk/Views/Footer/testpage

What i'm after is removing the "/Views/Footer" from the URL so it loks like:

http://www.mysite.co.uk/testpage

I have no idea how to do this. Could someone please give me step by step guide on code to use and where to put it so that it does this.

when ever i try double clicking on my Global.asax file it automatically opens the Global.asax.cs file which i suspect is also wrong


Solution

  • Add reference to system.web.routing to project

    add urlroutingmodule to http module in config:

        <configuration> 
       ... 
    
       <system.web> 
          ... 
          <httpModules> 
             ... 
             <add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web.Routing, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /> 
          </httpModules>
       </system.web> 
    
       <system.webServer> 
          <validation validateIntegratedModeConfiguration="false"/> 
          <modules> 
             ... 
             <add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web.Routing, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /> 
          </modules> 
    
    
          <handlers>
             ...
             <add name="UrlRoutingHandler" preCondition="integratedMode" verb="*" path="UrlRouting.axd" type="System.Web.HttpForbiddenHandler, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
          </handlers>
          ... 
       </system.webServer>
    </configuration>
    

    Define routes in global.asax:

    void Application_Start(object sender, EventArgs e) 
    { 
       RegisterRoutes(RouteTable.Routes); 
    } 
    
    void RegisterRoutes(RouteCollection routes) 
    { 
       // Register a route for Categories/All 
       routes.Add( 
          "All Categories",
             new Route("Categories/All", new CategoryRouteHandler()) 
          );
    
       // Register a route for Categories/{CategoryName} 
       routes.Add( 
          "View Category",
          new Route("Categories/{*CategoryName}", new CategoryRouteHandler()) 
       );
    
       // Register a route for Products/{ProductName} 
       routes.Add( 
          "View Product",
          new Route("Products/{ProductName}", new ProductRouteHandler()) 
       );
    }
    

    Create route handler classes

    public class ProductRouteHandler : IRouteHandler 
    { 
       public IHttpHandler GetHttpHandler(RequestContext requestContext) 
       { 
          string productName = requestContext.RouteData.Values["ProductName"] as string; 
    
          if (string.IsNullOrEmpty(productName)) 
             return Helpers.GetNotFoundHttpHandler(); 
          else 
          { 
             // Get information about this product 
             NorthwindDataContext DataContext = new NorthwindDataContext(); 
             Product product = DataContext.Products.Where(p => p.ProductName == productName).SingleOrDefault(); 
    
             if (product == null) 
                return Helpers.GetNotFoundHttpHandler(); 
             else 
             { 
                // Store the Product object in the Items collection 
                HttpContext.Current.Items["Product"] = product; 
    
                return BuildManager.CreateInstanceFromVirtualPath("~/ViewProduct.aspx", typeof(Page)) as Page; 
             } 
          } 
       } 
    } 
    

    create asp.net pages that process request:

    protected void Page_Load(object sender, EventArgs e) 
    { 
       dvProductInfo.DataSource = new Product[] { Product }; 
       dvProductInfo.DataBind(); 
    } 
    
    protected Product Product 
    { 
       get 
       { 
          return HttpContext.Current.Items["Product"] as Product; 
       } 
    }
    

    This is a good reference to work off, I have used this in the past on webforms apps and it worked like a charm.