1

I am going through a weird problem right now regarding routing in ASP.NET WebForms. The only reason I am trying to use routing is to achieve better looking URLs and nothing else. Also, this is the first time ever I am trying to implement routing.

My goal is simply: www.domain.com/default.aspx --> www.mydomain.com/Home-Page

So, I have implemented the following in my global.asax

Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
    RegisterRoutes(RouteTable.Routes)
End Sub

and

Sub RegisterRoutes(ByVal routes As RouteCollection)
    routes.MapPageRoute("homepage", "Home-Page", "~/Default.aspx")
End Sub

The goal is achieved however, all the images on my pages disappeared for logical reasons; instead of locating img/someimage.png it was trying to get Home-Page/img/someimage.png

Now, I can of course correct the path for my images but that is going to take quite a long time. I would gladly get rid of routing instead.

My question: Is there any way to prevent folders (images, css, js etc.) from getting affected by the routing mechanism and only achieve nice URLs?

8
  • Take a look at this question: stackoverflow.com/questions/5674329/… Commented Jan 21, 2012 at 10:48
  • Those extensions don't normally get processed by the asp.net handlers, so wouldn't normally be affected by routing. Have you configured IIS to force all file types through to asp.net? Commented Jan 21, 2012 at 10:57
  • Is it actually routing the images? Or is it just trying to access them relative to the page? Maybe you just need to make them absolute to the website root e.g. "~/img/someimgage.png"? Commented Jan 21, 2012 at 11:12
  • @TiesonT. thank your for the link. Yes I did check it and the solution there did not work. Commented Jan 21, 2012 at 11:49
  • 1
    @DaleBurrell This was a very interesting, certainly not expected and a working answer that shows sometimes we have to change the angle we look at things.. I'd have never thought at looking anywhere other than my global.asax and hence you are here with a simple html solution.. incredible! thank you very much.. Commented Jan 21, 2012 at 12:38

4 Answers 4

3

You can tell routing to ignore specific file types. Make sure this entry comes before the specific routes.

routes.IgnoreRoute("{file}.png")
Sign up to request clarification or add additional context in comments.

1 Comment

For me, routes.Ignore worked and IgnoreRoute was not found. May be it is replaced in newer versions.
0

OK.. Strange enough but I seem to find a solution:

first of all, just to let you know, @DaleBurrell's suggestion on using html tag also worked.

I changed my navigation to read from:

<a id="default" href="Home/">Home</a>

to this:

<a id="default" href="<%: Page.GetRouteUrl("home", vbnull)%>" >Home</a>

basically, instead of hard coding the url of my main navigation, I am grabbing the actual name value from global.asax.

in global asax:

Sub RegisterRoutes(ByVal routes As RouteCollection)
    routes.MapPageRoute("home", "Home", "~/Default.aspx")
End Sub

everything seems fine and working right now.. I'll post here if anything else comes up...

Comments

0

Just add to the ScriptManager the stuff you wish to exclude

  <asp:ScriptManager ID="ScriptManager1" runat="server">
      <Scripts>
        <asp:ScriptReference Path="jquery-1.7.1.min.js" />
        <asp:ScriptReference Path="init.js" />
      </Scripts>
  </asp:ScriptManager>

Comments

0

I had this problem... I was implementing routes so my urls would look nice for search engines... (something like categories/beans-pulses/3001 instead of products.aspx?catID=3001) by including

        routes.MapPageRoute(
           "Category-Simple-Route",
           "categories/{seo-catName}/{CatId}",
           "~/products.aspx"
           );

in the Application_Start function.

I had made the mistake to think that defining page routes like this would not interfere with the directory structure, which proved wrong. Even though products.aspx is used in the above example it is treated as a file called 3001 in the sub-folder categories.

The other mistake I had made was to make all my file references, including src on img tags, ajax url calls, script tags and all href links relative. As img and scripts etc are in fixed locations it made sense to give them explicit references ie. /img/image.jpg and not img/image.jpg (addition of leading slash and similar for links. Asp: tags with a ImageUrl parameter to start ~/. After an hour with Search & Replace everything worked fine.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.