Rewrite URLS using global.asax

13,284

Solution 1

Why do you want use rewriting, when you can do it really easy using asp.net routing?

Please, look at the following link for more info: http://msdn.microsoft.com/en-us/library/cc668201.aspx

Solution 2

You should access HttpApplication.Context. Here is how I do it (C#):

string reqPath = Request.Url.AbsolutePath;
if(reqPath=="/")
    newPath="/Pages/PL/Main.aspx";
if (newPath != "")
    HttpApplication.Context.RewritePath(newPath);

As I can see in documentation you should be able to use exactly the same syntax to access the context in VB.NET.

You could also use II7 url rewrite module if you really wanted.

Share:
13,284
MissCoder87
Author by

MissCoder87

Updated on June 11, 2022

Comments

  • MissCoder87
    MissCoder87 almost 2 years

    I'm trying to make some friendlyurls in my vb.net (.net 4) project and I'm trying to do it using something I read about global.asax and Application_Beginrequest but I can't get it to compile.

    Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
        Dim httpContext As System.Web.HttpContext = httpContext.Current
        Dim currentURL As String = currentURL.Request.Path.ToLower()
        If currentURL.IndexOf("widgets") > 0 Then
            objHttpContext.RewritePath("products.aspx?ID=123")
        Else
            objHttpContext.RewritePath(httpContext)
        End If
    End Sub
    

    Above is what i'm trying but it's erroring on the objHttpContext. is there another method? Ideally once I get the above method working I'm going to be attempting to use a database call to work out the URLs. So any suggestions in that direction will also be very welcome. I'm trying to get away from having to install anything on IIS as it's a load balenced enviroment that I'd rather not install something on every server.

    Thanks

    Tom

  • walther
    walther about 12 years
    What is the advantage over asp.net routing if I may ask?
  • Maciej
    Maciej about 12 years
    @walther: Routing is an MVC feature not standard ASP.NET. Question did not mention MVC.
  • walther
    walther about 12 years
    See, this is why you're wrong. Routing is part of webforms as well since .NET 4, which OP stated he's using as well. Don't be stuck in the past, .NET constantly evolves and we need to keep up with it :)
  • Maciej
    Maciej about 12 years
    A little harsh comment but taken. Learned something important. ASP.NET is not my main speciality.
  • walther
    walther about 12 years
    I'm sorry if it sounded harsh. I'll definitely try to be more polite next time. Thank you for letting me know. Maybe I learned something important too :)
  • mustafa öztürk
    mustafa öztürk over 10 years
    ... "routing to provide features that are used only in MVC applications and in Dynamic Data applications"
  • walther
    walther over 10 years
    @mustafaöztürk, refresh your knowledge, routing is applicable to webforms as well.