473,322 Members | 1,417 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Correct non page class?

I have several pages that need to use a method I've written that
streams in text file content. I added a CS file to my website and
named it Utilities. It has two static methods. One method reads in a
file. The other method passes a particular text file path. It is
similar to:

public static string BookList(System.Web.HttpServerUtility pServer){
{
return texturizeFileContent(@"content\fiction2005BookList .txt",
pServer).ToString();
}
}

public static StringBuilder texturizeFileContent(string pFilePath,
System.Web.HttpServerUtility pServer){

StreamReader sr = new StreamReader(pServer.MapPath(pFilePath));
//other code here }
In any ASPX page that needs to access this method, I do this:

lblBookList.Text = Utilities.BookList(Server);

I'm passing in a System.Web.HttpServerUtility object because I wasn't
sure how else to get a MapPath(). Any suggestions?

I'll add more static methods as more text files are added. The above
method works and keeps me from chasing down file paths, should they
ever change. Is this the correct way to allow multiple pages to access
a non page class?

Thanks,
Brett

Nov 22 '06 #1
1 1082
You can use HttpContext.Current ...which'll be null for non-web requests so
you won't be able to re-use that class in a winform (for example)...not like
you can re-use it now anyways..

HttpContext context = HttpContext.Current;
if (context == null)
{
throw new Exception("Method cannot be used outside of web context");
}
using (StreamReader sr = new StreamReader(context.Server.MapPath(filePath))
{
...
}
notice how I also used using and fixed your variable naming ;)
(j/k...sorta...)

Karl
--
http://www.openmymind.net/
http://www.codebetter.com/
"brett" <ac*****@cygen.comwrote in message
news:11**********************@k70g2000cwa.googlegr oups.com...
>I have several pages that need to use a method I've written that
streams in text file content. I added a CS file to my website and
named it Utilities. It has two static methods. One method reads in a
file. The other method passes a particular text file path. It is
similar to:

public static string BookList(System.Web.HttpServerUtility pServer){
{
return texturizeFileContent(@"content\fiction2005BookList .txt",
pServer).ToString();
}
}

public static StringBuilder texturizeFileContent(string pFilePath,
System.Web.HttpServerUtility pServer){

StreamReader sr = new StreamReader(pServer.MapPath(pFilePath));
//other code here }
In any ASPX page that needs to access this method, I do this:

lblBookList.Text = Utilities.BookList(Server);

I'm passing in a System.Web.HttpServerUtility object because I wasn't
sure how else to get a MapPath(). Any suggestions?

I'll add more static methods as more text files are added. The above
method works and keeps me from chasing down file paths, should they
ever change. Is this the correct way to allow multiple pages to access
a non page class?

Thanks,
Brett
Nov 22 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Keith Chadwick | last post by:
I am migrating an existing ASP application over to .NET as a learning exercise. The existing application uses a lot of xsl and xml transformations to render each page. Stored as an application...
2
by: Randal Chapman | last post by:
I just started getting a strange error in my asp.net project. One page is returning the parser error of cannot load type. I have checked and re-checked the inherits statement = namespace.class name...
2
by: Ranginald | last post by:
Hi, I have two pages. Default.aspx ....Codefile="default.aspx.cs" Inherits="Web" %> and Sculpture.aspx
0
by: sehguh | last post by:
Hiya Folks, I am Currently using windows xp. Also using Visual Web Developer 2005 and Microsoft Sql server 2005. The main page consists of an aspx page and a master page. The page also...
1
by: sehguh | last post by:
Hello folks I have recently been studying a book called "sams teach yourself asp.net 2.0 in24 hours by scott mitchell. I have reached page 614 but when i tried to run an asp page called...
1
by: kang jia | last post by:
hi currently i am editing signup page, when user enter deupicated NRIC and click signup, they will go to do_signuppage and read the error message and then after 5 seconds, they will be redirected...
1
by: kang jia | last post by:
hi when user entered particulars in signuppage and click" signup" button, i will direct them to do_signup.php. if say the NRIC is dupicate in datebase, i will redirect them back to signup page...
9
by: seep | last post by:
hi i m finding following error on the code that i wants to use to get all record from table via store procedure with paging. the error is : Input string was not in a correct...
3
osward
by: osward | last post by:
Hi, everyone, I had managed to make use of the date link from a simple calendar script to my query table. When I click on the date's link or Prev and Next Month link, The table first row will be...
1
by: differentsri | last post by:
THIS IS AN ASP.NET 1.1 APPLICATION IAM TRYING TO UPDATE THE FIELD BUT I AM NOT ABLE TO UPDATE IT? CAN U TELL THE REASON ? IT IS GIVING THE FOLLOWING ERROR BELOW I HAVE ALSO GIVEN THE CODE OF...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.