Jump to content

Help an ASP.Net Newbie


Yahweh
 Share

Recommended Posts

Years and years ago I started programming in VB, then I moved to classic ASP, and I'm perfectly fluent in both languages. Only recently I've decided to move on to ASP.Net, because I figure that classic ASP will die and be forgotten in the next few years.So now I feel like a n00b again. I've never touched web controls before, and I don't know what any of the namespaces are, so I'm going to reserve this thread for all of my "please help me, I'm a total n00b" questions about ASP.Net :) If someone wants to answer my questions, presume I have zero experience with ASP.Net; too many of the tutorials I've seen online are not written with beginners in mind, so they can't really help me out at all.First, I want to know how ASP.Net does page cacheing. When I use ASP, I have to jump through hoops to make pages cache correctly (first by creating a FileScriptingObject to see if a page is expired, then creating a BuildPage function to write a page to disk, then Server.Transfer-ing over to the page I've just created). Page cacheing in ASP is no fun at all.When I searched on Google, I came across this tutorial at 4Guys, and it looks like cacheing can be done in just one line of code. However, I have a question about this: are the page caches stored in server memory, or stored on disk?I just noticed most of the tutorials mentioned that the duration of time for page caching is in seconds; but I like to cache my pages for long term storage, sometimes for months at a time, and I can't imagine that its very good to cache pages (especially huge ones) in memory for that amount of time. I prefer to have my caches stored on disk, so that I don't use up all of my system resources.Second, how can I execute regular expressions with ASP.Net.Just recently, I wroted a 400+ character length regular expression in ASP that simulates a recursive function, and it is incredibly slow and runs out of memory very quickly; the same expression can be executed in a few characters using balanced groups in ASP.Net. The .NET regular expressions are much more powerful than anything available in ASP, so I prefer to use them.

Link to comment
Share on other sites

Second, how can I execute regular expressions with ASP.Net.
use the System.Text.RegularExpressions namespace.C#
string newString = Regex.Replace(originalString,"regular expression here");

VB

Dim newString As String newString = Regex.Replace(originalString, "regular expression here")

As for caching pages...I am almost positive it caches to memory, so yeah, it would not be a good idea to cache many pages for long periods of time (beyond a couple of hours).

Link to comment
Share on other sites

As for caching pages...I am almost positive it caches to memory, so yeah, it would not be a good idea to cache many pages for long periods of time (beyond a couple of hours).

I suppose then I'm forced to cache files to disk by writing my own functions. So, how do I (1) read a file as ASCII text and (2) write a file to disk. :)
Link to comment
Share on other sites

use the System.Text.RegularExpressions namespace.VB
Dim newString As String newString = Regex.Replace(originalString, "regular expression here")

I like to reuse objects over and over, and in particular I am trying to create a Regex object whose .pattern property I can modify over and over. Here is a short script I've written:
<%@ Page Language="VB" Explicit="True" %><script runat="server">Public Function GetFileName(strString)    Dim myRegex as Regex = New Regex("[^a-zA-Z0-9]")    strString = myRegex.Replace(strString, "_") & ".html"    'On the first pass, article.asp?id=50〈=en becomes    'article_asp_id_50_lang_en.html    myRegex.Pattern = "_asp"    strString = myRegex.Replace(strString, "")    'On the second pass, article_asp_id_50_lang_en.html becomes    'article_id_50_lang_en.html    GetFileName = strStringEnd FunctionSub Page_Load (sender as Object, e as EventArgs)    lblRegex.Text = GetFileName(request.ServerVariables("SCRIPT_NAME") &  _                                "?" & request.ServerVariables("QUERY_STRING"))End Sub</script><form runat="server"><asp:Label id="lblRegex" runat="server" /></form>

I am converting a filename to something else for use in a cache system I'm trying to write. If a user visits the page the article.asp?id=50〈=en, then the GetFileName function converts it to "article_id_50_lang_en.html".However, after I make my first replacement, I want to change the pattern to continue doing processing to it to remove a superfluous "_asp". However, I get the following error:

Compiler Error Message: BC30390: 'System.Text.RegularExpressions.Regex.pattern' is not accessible in this context because it is 'Protected'.Source Error:Line 7:      'article_asp_id_50_lang_en.htmlLine 8:  Line 9:      myRegex.Pattern = "_asp"Line 10:    strString = myRegex.Replace(strString, "")Line 11:    'On the second pass, article_asp_id_50_lang_en.html becomes
How do I make my expression "unprotected", so I can modify my pattern and other regex properties?
Link to comment
Share on other sites

I do not use Regex muxh, I am afraid I do not knowIt is basically saying Regex.Pattern is read only. I don't know if you can change that or not.

No worries.I figured out a solution, but it seems odd:
<%@ Page Language="VB" Explicit="True" %><%@ Import namespace="System.Text.RegularExpressions" %><script runat="server">    Public Function GetFileName(strString)        Dim re as Regex = New Regex("[^a-zA-z0-9]")         strString = "cache" & Re.Replace(strString, "_") & ".html"         'On the first pass, /article.asp?id=50〈=en becomes         'cache_article_aspx_id_50_lang_en.html                 re = New Regex ("_aspx")        strString = re.Replace(strString, "")         'On the second pass, cache_article_aspx_id_50_lang_en.html becomes         'cache_article_id_50_lang_en.html         GetFileName = strString    End Function    Sub Page_Load (sender as Object, e as EventArgs)         lblRegex.Text = GetFileName(request.ServerVariables("SCRIPT_NAME") &  _                                     "?" & request.ServerVariables("QUERY_STRING"))    End Sub</script><html><head></head><body>    <form runat="server">    <asp:Label id="lblRegex" runat="server" />    </form></body></html>

That gives the results I want, but I'm not sure if its proper code. Should I be using the New keyword each time I want a new pattern? (When I omit one or both of the New keywords, I get an error.)It seems like I'm creating two seperate objects, when I only need one. My pages are heavily dependent on Regex and use dozens of different patterns to get the formatting that I want, but I'm not sure if its very good to be creating dozens of new objects (in the ASP version, I only used one Regex object and modified its .pattern property whenever I needed).

Link to comment
Share on other sites

  • 2 weeks later...

I've figured out regexes and can work with them pretty easily now. Now, I'm having difficulty working with ADO.Net.Basically, I want to recreate these processes from ASP to ASP.Net:

'Create a new recordConn.OpenRS.Open "Select * From MyTable WHERE 0=1", Conn, 3, 3   RS.AddNew    RS("Field1") = SomeValue   RS("Field2") = AnotherValue   RS("Field3") = FinalValue   RS.UpdateRS.CloseConn.Close'Updating a recordConn.OpenRS.Open "Select * From MyTable WHERE ID = 5", Conn, 3, 3   RS("Field1") = SomeValue   RS("Field2") = AnotherValue   RS("Field3") = FinalValue   RS.UpdateRS.CloseConn.Close

Notice that I can update and add new records without using UPDATE and INSERT INTO sql commands. I like that method because I don't have to escape any of the values I put into my database, its much clearer to read than the SQL equivalent, and it gives me a little more control over what gets updated.However, I've searched and searched, but all the tutorials I've seen online update databases with SQL commands. I want to use the same ADO.Net equivalent to the above process (using SQL Server), so how do I programmatically create and update with ADO.Net, without using sql INSERT INTO and UPDATE?

Link to comment
Share on other sites

  • 2 weeks later...

I've got another problem: My server is currently running .NET Framework 1.1 on my machine, but I want to run .NET Framework 2.0 (I've already downloaded the latest version). How do I tell my server to compile pages under the 2.0 framework?Note: Some of the solutions I've seen online are specific to IIS 6.0, however I'm not running that program as my webserver (it didn't come with my WinXP Home edition). I'm using the micro-webserver that comes with WebMatrix.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...