locked
how to do not route images, css,js etc RRS feed

  • Question

  • User1570265326 posted

    hi. i have achived routing with using globa.asax


    but it does also routing css,images,js and this kind of files


    but i only want to route url for better seo


    i really need help

    it does not appear at me ignorerouting


    here is the my config


    <%@ Application Language="C#" %>
    <%@ Import Namespace="System.Web.Routing" %>
    <script runat="server">
    
    
        void Application_Start(object sender, EventArgs e) 
        {
            // Code that runs on application startup
            RegisterRoutes();
        }
    
        private static void RegisterRoutes()
        {
            RouteTable.Routes.RouteExistingFiles = false;
            System.Web.Routing.RouteTable.Routes.Add(
                    "pokemon", new System.Web.Routing.Route("Pokemon/{pokemon_id}",
                    new UserRouteHandler("~/Pokedex-Pokemon.aspx")));
          
       
        }



    public class RouteHandler : IRouteHandler
    {
        public RouteHandler()
        {
    
        }
        public RouteHandler(string virtualPath)
        {
            _virtualPath = virtualPath;
        }
    
        public IHttpHandler GetHttpHandler(RequestContext requestContext)
        {
    
    
    
            var display = BuildManager.CreateInstanceFromVirtualPath(
                            _virtualPath, typeof(Page)) as IDisplay;
            return display;
        }
    
         string _virtualPath;
    }





    public class UserRouteHandler : IRouteHandler
    {
        public UserRouteHandler()
        {
    
        }
        public UserRouteHandler(string virtualPath)
        {
            _virtualPath = virtualPath;
        }
    
        public IHttpHandler GetHttpHandler(RequestContext requestContext)
        {
    
    
    
            if (requestContext.RouteData.Values["pokemon_id"] != null)
            {
                var display = BuildManager.CreateInstanceFromVirtualPath(
                                _virtualPath, typeof(Page)) as IUserDisplay;
                if (display != null)
                {
                    display.GetPokemonId = requestContext.RouteData.Values["pokemon_id"] as string;
                    return display;
                }
                else
                {
                    return null;
                }
            }
            else
            {
                return null;
            }
        }
    
         string _virtualPath;
    }
    


    Monday, March 15, 2010 6:07 PM

Answers

  • User197322208 posted

    <link rel="stylesheet" href="main_menu_not_logged_tr_files/cbcscbmain_menut_logged_tr.css" type="text/css" />  

    <style type="text/css">
            @import '<%= ResolveUrl("~/main_menu_not_logged_tr_files/cbcscbmain_menut_logged_tr.css") %>';
     
        </style>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 16, 2010 9:54 AM
  • User197322208 posted

    for js use

    <script type="text/javascript" src='<%= ResolveUrl("~/path to script ")%>'></script>


    And for iframe the same for script

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 16, 2010 10:19 AM

All replies

  • User401360897 posted

    it does also routing css,images,js and this kind of files

    This is not possible because as far i know ASP.NET MVC routing automatically check the existing file first.

    Here is what Steven Sanderson said in his book,

    However, the routing system still does check the file system to see if an
    incoming URL happens to match a file or disk, and if so, routing ignores the request (bypassing
    any route entries that the URL might also match) so that the file will be served directly.
    This is very convenient for static files, such as images, CSS, and JavaScript files. You can
    keep them in your project (e.g., in your /Content or /Script folders), and then reference and
    serve them directly, just as if you were not using routing at all. Since the file genuinely exists
    on disk, that takes priority over your routing configuration.

    If, instead, you want your routing configuration to take priority over files on disk, you can set
    the RouteCollection’s RouteExistingFiles property to true. (It’s false by default.)


    Monday, March 15, 2010 10:53 PM
  • User1904378495 posted

    Why do you think it's routing the css, js and image files? as @imran_ku07 pointed out, by default those files aren't going through the routing engine and just served up.

    Monday, March 15, 2010 11:30 PM
  • User197322208 posted

    You are routing css, js and any other files because of the line :

     RouteTable.Routes.RouteExistingFiles = false;  

    In order to ignore css files, please put

    RouteTable.Routes.IgnoreRoute("{resource}.css/{*pathInfo}");

    ( the same for js)

    In order to ignore all content folder , you can do :

    RouteTable.Routes.IgnoreRoute("{folder}/{*pathInfo}", new {folder="content"});

    ( same for scripts)

    Tuesday, March 16, 2010 3:02 AM
  • User1570265326 posted

    You are routing css, js and any other files because of the line :

     RouteTable.Routes.RouteExistingFiles = false;  

    In order to ignore css files, please put

    RouteTable.Routes.IgnoreRoute("{resource}.css/{*pathInfo}");

    ( the same for js)

    In order to ignore all content folder , you can do :

    RouteTable.Routes.IgnoreRoute("{folder}/{*pathInfo}", new {folder="content"});

    ( same for scripts)

    thnx for the replay


    i checked so many topic about routing

    there were all the time this IgnoreRoute,


    but i dont have any option like that


    what may be the problem ?


    when i typed that microsoft visual studio 2008 sp1 does not recognize it :/

    Tuesday, March 16, 2010 6:09 AM
  • User197322208 posted

    but i dont have any option like that

    You have already

    RouteTable.Routes.RouteExistingFiles = false;


    put immediately after

    RouteTable.Routes.IgnoreRoute("{resource}.css/{*pathInfo}");

    RouteTable.Routes.IgnoreRoute("{folder}/{*pathInfo}", new {folder="content"});


    and compile. If there are any errors , please tell

    http://msdn.microsoft.com/en-us/library/dd505203%28VS.100%29.aspx


    Tuesday, March 16, 2010 6:14 AM
  • User1570265326 posted

    but i dont have any option like that

    You have already

    RouteTable.Routes.RouteExistingFiles = false;


    put immediately after

    RouteTable.Routes.IgnoreRoute("{resource}.css/{*pathInfo}");

    RouteTable.Routes.IgnoreRoute("{folder}/{*pathInfo}", new {folder="content"});


    and compile. If there are any errors , please tell

    http://msdn.microsoft.com/en-us/library/dd505203%28VS.100%29.aspx


    thnx for the replay again


    but it gives error :/



    Tuesday, March 16, 2010 6:52 AM
  • User401360897 posted

    these are in System.Web.Mvc namespace

    <%@ Import Namespace="System.Web.Mvc" %>

    But i suggest you please read my Previous Post

    Tuesday, March 16, 2010 6:59 AM
  • User1570265326 posted

    ok this is my final code but it is still routing images etc


            RouteTable.Routes.RouteExistingFiles = false;
    
            RouteTable.Routes.IgnoreRoute("{resource}.css/{*pathInfo}");
    
            RouteTable.Routes.IgnoreRoute("{folder}/{*pathInfo}", new { folder = "content" });
            
            System.Web.Routing.RouteTable.Routes.Add(
                    "pokemon", new System.Web.Routing.Route("Pokedex/{pokemon_id}/{pokemon_name}",
                    new UserRouteHandler("~/Pokedex-Pokemon.aspx")));


    Tuesday, March 16, 2010 7:05 AM
  • User197322208 posted

    are you missing an assembly reference?

    Yes... do you add the MVC reference? Did you start with a MVC project ?


    Tuesday, March 16, 2010 7:12 AM
  • User1570265326 posted

    are you missing an assembly reference?

    Yes... do you add the MVC reference? Did you start with a MVC project ?



    i am adding my existing project


    now added mvc refenc

    my final code is like this


    now css is working fine but images i couldnt manage to fix it

    my images is in a folder called images in the root directory


            RouteTable.Routes.RouteExistingFiles = false;
    
            RouteTable.Routes.IgnoreRoute("{Buttons}.css/{*pathInfo}");
    
    
            RouteTable.Routes.IgnoreRoute("{images}/{*pathInfo}", new { folder = "images" });
            
            System.Web.Routing.RouteTable.Routes.Add(
                    "pokemon", new System.Web.Routing.Route("Pokedexx/{pokemon_id}/{pokemon_name}",
                    new UserRouteHandler("~/Pokedex-Pokemon.aspx")));


      





    Tuesday, March 16, 2010 7:14 AM
  • User1570265326 posted

    IgnoreRoute for css is working fine since i have stated its name clearly


    but i want also to exlcude folders


    i really need help about this


    i appreciate all answers ty

    Tuesday, March 16, 2010 7:35 AM
  • User401360897 posted

    RouteTable.Routes.IgnoreRoute("images/{*pathInfo}"); 

    Tuesday, March 16, 2010 7:41 AM
  • User1570265326 posted

    RouteTable.Routes.IgnoreRoute("images/{*pathInfo}"); 


    still not working


    my images folders has sub folders

    also i am using


    these two

    do i  have anything wrong ?

    do i need to use mvc version 1.0 instead of 2.0 ?




     <add assembly="System.Web.Routing, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
    
    <add assembly="System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
    
    
    <%@ Import Namespace="System.Web.Routing" %>
    <%@ Import Namespace="System.Web.Mvc" %>



    <%@ Application Language="C#" %>
    <%@ Import Namespace="System.Web.Routing" %>
    <%@ Import Namespace="System.Web.Mvc" %>
    <script runat="server">
    
    
        void Application_Start(object sender, EventArgs e) 
        {
            // Code that runs on application startup
            RegisterRoutes();
        }
    
        private static void RegisterRoutes()
        {
            RouteTable.Routes.RouteExistingFiles = false;
    
            RouteTable.Routes.IgnoreRoute("{Buttons}.css/{*pathInfo}");
            RouteTable.Routes.IgnoreRoute("main_menu_not_logged_en_files/{*pathInfo}");
            RouteTable.Routes.IgnoreRoute("main_menu_not_logged_tr_files/{*pathInfo}");
    
            RouteTable.Routes.IgnoreRoute("images/{*pathInfo}");
            
            System.Web.Routing.RouteTable.Routes.Add(
                    "pokemon", new System.Web.Routing.Route("Pokedexx/{pokemon_id}/{pokemon_name}",
                    new UserRouteHandler("~/Pokedex-Pokemon.aspx")));
          
       
        }





    Tuesday, March 16, 2010 7:47 AM
  • User1570265326 posted

    RouteTable.Routes.IgnoreRoute("images/{*pathInfo}"); 



    still not working what may be the problem :/

    Tuesday, March 16, 2010 8:24 AM
  • User401360897 posted

    Put this very top

    RouteTable.Routes.IgnoreRoute("images/{*pathInfo}"); 

    Tuesday, March 16, 2010 8:32 AM
  • User197322208 posted

    RouteTable.Routes.IgnoreRoute("images/{*pathInfo}"); 



    still not working what may be the problem :/

    RouteTable.Routes.IgnoreRoute("{folder}/{*pathInfo}"new { folder = "images" }); 

    Tuesday, March 16, 2010 8:37 AM
  • User1570265326 posted

    Put this very top

    RouteTable.Routes.IgnoreRoute("images/{*pathInfo}"); 




    <%@ Application Language="C#" %>
    <%@ Import Namespace="System.Web.Routing" %>
    <%@ Import Namespace="System.Web.Mvc" %>
    <script runat="server">
    
    
        void Application_Start(object sender, EventArgs e) 
        {
            RegisterRoutes();
        }
    
        private static void RegisterRoutes()
        {
            RouteTable.Routes.IgnoreRoute("images/{*pathInfo}");  
    
            System.Web.Routing.RouteTable.Routes.Add(
                    "pokemon", new System.Web.Routing.Route("Pokedexx/{pokemon_id}/{pokemon_name}",
                    new UserRouteHandler("~/Pokedex-Pokemon.aspx")));
    
        }







    here is the my web config

    i tried with version 1.0 and 2.0 still does not work


    				<add assembly="System.Web.Routing, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
    				<add assembly="System.Web.Mvc, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>



    Tuesday, March 16, 2010 8:40 AM
  • User1570265326 posted

    RouteTable.Routes.IgnoreRoute("images/{*pathInfo}"); 



    still not working what may be the problem :/

    RouteTable.Routes.IgnoreRoute("{folder}/{*pathInfo}"new { folder = "images" }); 



    this did not work too Cry

    Tuesday, March 16, 2010 8:43 AM
  • User197322208 posted

    also please put for images the src like this

    <img src= '<% ResolveUrl("~/images/image.jpg")%>' >

    Tuesday, March 16, 2010 8:45 AM
  • User1570265326 posted

    also please put for images the src like this

    <img src= '<% ResolveUrl("~/images/image.jpg")%>' >


    this is not the solution i am looking for


    also i dont understand


    at every topic i look they put this for preventing files to be routed like images html files


    but it does not work at me :/


           RouteTable.Routes.IgnoreRoute("{resource}.axd/{*pathInfo}");



    Tuesday, March 16, 2010 8:52 AM
  • User1570265326 posted

    here is the run time preview




    Tuesday, March 16, 2010 8:57 AM
  • User197322208 posted

    also please put for images the src like this

    <img src= '<% ResolveUrl("~/images/image.jpg")%>' >


    this is not the solution i am looking for

    My dear

    Even if you do not route existing files (AND NOW you are not routing , as I see from the image) you must put the resolveurl.

    Why ?

    Because, when you are in http://../bitirme/pokedex/asd/1234

    and in the html page is

    <img src ="images/image.jpg"

    the browser interprets that must look into

    http://../bitirme/pokedex/asd/1234/images/image.jpg

    That's why UrlResolve exists (like Server.MapPath ...)


    Tuesday, March 16, 2010 9:03 AM
  • User401360897 posted

    RouteTable.Routes.IgnoreRoute("bitirme/Pokedexx/123/images/pokedximages/{*pathInfo}");

    Tuesday, March 16, 2010 9:03 AM
  • User1570265326 posted

    also please put for images the src like this

    <img src= '<% ResolveUrl("~/images/image.jpg")%>' >


    this is not the solution i am looking for

    My dear

    Even if you do not route existing files (AND NOW you are not routing , as I see from the image) you must put the resolveurl.

    Why ?

    Because, when you are in http://../bitirme/pokedex/asd/1234

    and in the html page is

    <img src ="images/image.jpg"

    the browser interprets that must look into

    http://../bitirme/pokedex/asd/1234/images/image.jpg

    That's why UrlResolve exists (like Server.MapPath ...)



    i tried the way you said

    but image is not shown


    here browser code and my codebehind code


    since my pages are completey dynamic i  have to compose images like this


         lblPokmemonImage.Text = "<img src='<% ResolveUrl(\"~/images/pokemons/" + dsPokedex.Tables[0].Rows[0]["PokemonId"].ToString() +
                   dsPokedex.Tables[0].Rows[0]["Name"].ToString().Replace(" ", "").Replace(".", "") + ".png\")%>'" +
                " title=\"" + dsPokedex.Tables[0].Rows[0]["Name"].ToString() + " (Pokemon)\" " +
                " alt=\"" + dsPokedex.Tables[0].Rows[0]["Name"].ToString() + " (Pokemon)\"  />";








    Tuesday, March 16, 2010 9:31 AM
  • User197322208 posted


  • lblPokmemonImage.Text = "<img src='" + ResolveUrl("~/images/pokemons/" + dsPokedex.Tables[0].Rows[0]["PokemonId"].ToString() +  
  •           dsPokedex.Tables[0].Rows[0]["Name"].ToString().Replace(" """).Replace(".""") + ".png\")' ) +  
  •        " title=\"" + dsPokedex.Tables[0].Rows[0]["Name"].ToString() + " (Pokemon)\" " +  
  •        " alt=\"" + dsPokedex.Tables[0].Rows[0]["Name"].ToString() + " (Pokemon)\"  />";  


    And please,please do not use code behind in ASP.NET MVC

Tuesday, March 16, 2010 9:36 AM
  • User1570265326 posted


  • lblPokmemonImage.Text = "<img src='" + ResolveUrl("~/images/pokemons/" + dsPokedex.Tables[0].Rows[0]["PokemonId"].ToString() +  
  •           dsPokedex.Tables[0].Rows[0]["Name"].ToString().Replace(" """).Replace(".""") + ".png\")' ) +  
  •        " title=\"" + dsPokedex.Tables[0].Rows[0]["Name"].ToString() + " (Pokemon)\" " +  
  •        " alt=\"" + dsPokedex.Tables[0].Rows[0]["Name"].ToString() + " (Pokemon)\"  />";  


    And please,please do not use code behind in ASP.NET MVC

  • thnx for the replay


    this is working after i corrected the code


           lblPokmemonImage.Text = "<img src=" +ResolveUrl("~/images/pokemons/" + dsPokedex.Tables[0].Rows[0]["PokemonId"].ToString() +
               dsPokedex.Tables[0].Rows[0]["Name"].ToString().Replace(" ", "").Replace(".", "") + ".png")+
            " title=\"" + dsPokedex.Tables[0].Rows[0]["Name"].ToString() + " (Pokemon)\" " +
            " alt=\"" + dsPokedex.Tables[0].Rows[0]["Name"].ToString() + " (Pokemon)\"  />"; 


    i can edit all images but also i have problem with my master page references :D


    can you also tell me how to fix these kind of refs


    <link rel="stylesheet" href="main_menu_not_logged_tr_files/cbcscbmain_menut_logged_tr.css" type="text/css" />


    Tuesday, March 16, 2010 9:45 AM
  • User197322208 posted

    <link rel="stylesheet" href="main_menu_not_logged_tr_files/cbcscbmain_menut_logged_tr.css" type="text/css" />  

    <style type="text/css">
            @import '<%= ResolveUrl("~/main_menu_not_logged_tr_files/cbcscbmain_menut_logged_tr.css") %>';
     
        </style>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 16, 2010 9:54 AM
  • User1570265326 posted

    <link rel="stylesheet" href="main_menu_not_logged_tr_files/cbcscbmain_menut_logged_tr.css" type="text/css" />  

    <style type="text/css">
            @import '<%= ResolveUrl("~/main_menu_not_logged_tr_files/cbcscbmain_menut_logged_tr.css") %>';
     
        </style>



    thnx a lot for all answer

    also i am gonna fix image problem with this


    runat="server" src="~\main_menu_not_logged_tr_files/ebbtcbmain_menut_logged_tr1_0.gif"

    Tuesday, March 16, 2010 10:01 AM
  • User1570265326 posted

    one more thing

    i will import javascript with this right ?


    <script  type="text/javascript">
            @import '<%= ResolveUrl("~/main_menu_not_logged_tr_files/cbjscbmain_menut_logged_tr.js") %>';
    </script>


    also how can i import iframe htm files


        <iframe  frameborder="0"   style=" width:62px; height:32px;" scrolling="no" src="Paypal.htm"></iframe> 


    Tuesday, March 16, 2010 10:06 AM
  • User197322208 posted

    for js use

    <script type="text/javascript" src='<%= ResolveUrl("~/path to script ")%>'></script>


    And for iframe the same for script

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 16, 2010 10:19 AM
  • User1570265326 posted

    i solved javascript with this way


    working very well



    <script language="javascript" src="<%=ResolveUrl("~/main_menu_not_logged_tr_files/cbjscbmain_menut_logged_tr.js")%>" type="text/javascript"></script>


    Tuesday, March 16, 2010 10:36 AM
  • User1570265326 posted

    i spent to much time to solve problems


    but at the end i can not use this method as i wished


    because my all paths are relative

    all images etc


    this would take huge time to fix them

    even if i do fix most of them i wont be able to fix my menus javascript files because it is generated by a software and very complicated


    :/


    so i am gonna use this method


    domain.com/variable1-variable2-variable3

    Tuesday, March 16, 2010 1:32 PM
  • User1570265326 posted

    here you can check my final result


    check pokemon name urls :)

    Pokedex

    Wednesday, March 17, 2010 11:53 AM