Rewrite engine in VB.NET RRS feed

  • Question

  • User2047813244 posted
    Hello all. I am new to C#, but have been coding in VB.NET and regular C for a few years. Anyway, I'm trying to write an URL rewriter in VB.NET, so I decided to use 15seconds's www.15seconds.com/issue/030522.htm (15seconds' Rewrite.NET) project and convert it into VB.Net. That way I'd have a kickass URL rewriting project, and I will become more familiar with C#. Here is the code in C#: using System; namespace Rewrite.NET { public class Rewrite : System.Web.IHttpModule { /// <summary> /// Init is required from the IHttpModule interface /// </summary> /// <param name="Appl"></param> public void Init(System.Web.HttpApplication Appl) { //make sure to wire up to BeginRequest Appl.BeginRequest+=new System.EventHandler(Rewrite_BeginRequest); } /// <summary> /// Dispose is required from the IHttpModule interface /// </summary> public void Dispose() { //make sure you clean up after yourself } /// <summary> /// To handle the starting of the incoming request /// </summary> /// <param name="sender"></param> /// <param name="args"></param> public void Rewrite_BeginRequest(object sender, System.EventArgs args) { //process rules here //cast the sender to a HttpApplication object System.Web.HttpApplication Appl=(System.Web.HttpApplication)sender; //load up the rules engine RulesEngine.Engine e = new RulesEngine.Engine(Appl); string r = e.Execute(); //only redirect if we have too if(r!="" && r.ToLower()!=RulesEngine.Engine.Getpath(Appl).ToLower()) SendToNewUrl(r, Appl); } public void SendToNewUrl(string url, System.Web.HttpApplication Appl) { Appl.Context.RewritePath(url); } } } Here is the VB code: Imports System Namespace Rewrite.NET Public Class Rewrite Inherits System.Web.IHttpModule Public Sub Init(ByVal Appl As System.Web.HttpApplication) Appl.BeginRequest += New System.EventHandler(Rewrite_BeginRequest) End Sub End Class End Namespace Now I think the colon in the class declaration means inheritace, however when I try to inherit the code in VB I get the error "Classes can only inherit from other classes". I've included the same references in both projects, but I don't know how to proceed. Also, the compiler has a problem with the first (and only) line in my Init subroutine. Can anyone give me a clue as to what's going on between my C# and VB versions? Any help is greatly appreciated.
    Wednesday, July 14, 2004 1:54 AM

All replies

  • User-1029435529 posted
    The colon (:) does sometimes equal Inherits in VB.NET. However, in this case, it equals Implements in VB.NET. So, your Class should start: Public Class Rewrite Implements System.Web.IHttpModule <color="#000000;width:550px;">Regarding the error in the first line of the Init subroutine, you cannot do a straight conversion of the C# syntax to VB.NET. The method of hooking up event handlers are completely different.</color> For you, using VB.NET, it would be: Public Overridable Sub Init(Appl As System.Web.HttpApplication) Implements System.Web.IHttpModule.Init AddHandler Appl.BeginRequest, AddressOf Me.Rewrite_BeginRequest End Sub I hope this helps.
    Sunday, July 25, 2004 10:41 AM
  • User-2113535000 posted
    Has anyone got a working example of this in VB.NET? The article and code on MSDN (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html/urlrewriting.asp) is great if you understand C#, but I need one in VB. I've google'd it but cannot find a good sample anywhere in VB.NET. Any help would be appreciated. Jimbob101
    Sunday, January 16, 2005 3:09 PM
  • User-1029435529 posted
    <color="#000000;width:550px;">Welcome to the ASP.NET Forums, Jimbob101. You're right, URL Rewriting articles using VB.NET are rare. In fact, I cannot recall ever seeing one. But this may prove to be a good thing for you. The code for rewriting is not long, so you can attempt the conversion yourself. This would be good for you for two reasons. 1) You get the hang of converting C# to VB.NET, which will allow you to benefit from any article you find, no matter what its coding language. 2) You will understand what the URL rewriting code is actually doing, which will help if you need to modify the code for your own particular purposes. If time is not pressing upon you, I'd recommend a manual conversion. That is, a line by line conversion from C# to VB.NET. This is a lot easier than you might think, as the differences between C# and VB.NET are largely a matter of syntax. If time is against you, you can try automatic conversions: C# to VB.NET Translator Of course, if you get stuck along the way, post the C# code that you cannot understand or cannot convert, and we will help you.</color>
    Sunday, January 16, 2005 9:03 PM
  • User-2113535000 posted
    Thanks for the advice, and the welcome! I'll have a go and get back to you with any problems.
    Wednesday, January 19, 2005 8:56 AM