473,378 Members | 1,427 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,378 software developers and data experts.

Reading from text file?

I have a main aspx file for my site and use web controls as includes
for content. The url may look like this:

www.abc.com/main.aspx?page1

I'd like to store content in a text file and depending on the url query
string, I'll know which text file to server up into main.aspx. How is
this done?

Also, I'm sure there is a better way to do this. Web controls seem
like over kill just for static content. I'm using .NET 2.0. What are
some suggestions?

Thanks,
Brett

Nov 9 '06 #1
9 1392
brett wrote:
I have a main aspx file for my site and use web controls as includes
for content. The url may look like this:

www.abc.com/main.aspx?page1

I'd like to store content in a text file and depending on the url query
string, I'll know which text file to server up into main.aspx. How is
this done?

Also, I'm sure there is a better way to do this. Web controls seem
like over kill just for static content. I'm using .NET 2.0. What are
some suggestions?

Thanks,
Brett
You don't have static content, since you have a parameter in your URL ;-)

If the text pages never change, you might want to make HTML pages out of
them and serve them as is. But otherwise, I don't think that what you do
is wrong.

Of course there are alternatives:

- First, you shouldn't use the query string as you do, without an
identifier for the parameter. By doing this, you're not complying with
standards, and more importantly, you close the door to future
developments of your page.If you do this:
www.abc.com/main.aspx?page=page1
then you have the possibility later to add something else without
breaking compatibility.
- Then, you might want to look into a CustomControl instead of a Web
Control. They're less heavy, because they are made of code behind only.
But if the existing works well, I wouldn't touch it.

HTH,
Laurent

--
Laurent Bugnion, GalaSoft
Software engineering: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
Nov 9 '06 #2
Thanks Laurent. A few questions.

1.) I see what you mean about a custom control and I like that idea.
It's just a cs file. But how would I know which page to serve up? The
query string lets me know that now.

2.) How can I get rid of the query string so that the url looks like
this:

www.abc.com/page1
www.abc.com/page2

but there really isn't a page1 or page2. It's just logic so I know
what to show. It's the same as

www.abc.com/index.aspx?p=page1
www.abc.com/index.aspx?p=page2

3.) I don't want to use HTML files because I have a shell for my site.
All pages are displayed within the this shell. It's easy with the
controls. I'm not sure how it is done with HTML files. That's why I'd
like to put content into a text file. Then I just add logic to the
control for displaying the text file content, depending on what is in
the URL is...or how ever else it is done.

Thanks,
Brett

Nov 9 '06 #3
Hi Brett,

brett wrote:
Thanks Laurent. A few questions.

1.) I see what you mean about a custom control and I like that idea.
It's just a cs file. But how would I know which page to serve up? The
query string lets me know that now.
The custom control has access to the Request. For example you can use
this.Page.Request (after checking of course that the Page is not null),
or using HttpContext.Current.Request.
2.) How can I get rid of the query string so that the url looks like
this:
You could do that easily by using URL mapping. In the web.config, add this:

<urlMappings enabled="true">
<clear />
<add
url="~/page1"
mappedUrl="~/index.aspx?p=page1" />
</urlMappings>

(etc for the other pages)
www.abc.com/page1
www.abc.com/page2

but there really isn't a page1 or page2. It's just logic so I know
what to show. It's the same as

www.abc.com/index.aspx?p=page1
www.abc.com/index.aspx?p=page2

3.) I don't want to use HTML files because I have a shell for my site.
All pages are displayed within the this shell. It's easy with the
controls. I'm not sure how it is done with HTML files. That's why I'd
like to put content into a text file. Then I just add logic to the
control for displaying the text file content, depending on what is in
the URL is...or how ever else it is done.

Thanks,
Brett
Totally understand, and totally agree. I don't see the point, in that
case, to generate HTML files. I was only mentioning this solution
because you seemed to want to come away from a dynamic solution.

HTH,
Laurent
--
Laurent Bugnion, GalaSoft
Software engineering: http://www.galasoft-LB.ch
Private/Malaysia: http://mypage.bluewin.ch/lbugnion
Support children in Calcutta: http://www.calcutta-espoir.ch
Nov 9 '06 #4
2.) How can I get rid of the query string so that the url looks like
this:

You could do that easily by using URL mapping. In the web.config, add this:

<urlMappings enabled="true">
<clear />
<add
url="~/page1"
mappedUrl="~/index.aspx?p=page1" />
</urlMappings>
Thanks again. It looks like urlMapping still needs the ?p=page syntax.
In other words, I'll still have to code that into links. I'd like any
link on my site that goes to another page on the site appear as

www.abc.com/section1
www.abc.com/section1/page1

So there isn't any ?p=page syntax anywhere.

Thanks,
Brett

Nov 9 '06 #5
Hi,

brett wrote:
>>2.) How can I get rid of the query string so that the url looks like
this:
You could do that easily by using URL mapping. In the web.config, add this:

<urlMappings enabled="true">
<clear />
<add
url="~/page1"
mappedUrl="~/index.aspx?p=page1" />
</urlMappings>

Thanks again. It looks like urlMapping still needs the ?p=page syntax.
In other words, I'll still have to code that into links. I'd like any
link on my site that goes to another page on the site appear as

www.abc.com/section1
www.abc.com/section1/page1

So there isn't any ?p=page syntax anywhere.

Thanks,
Brett
You know, suddenly I realize that what I wrote about urlMappings might
not even work... the problem is that non ASP.NET content is not passed
to the ASPNET module. So if the URL called doesn't have an ASPX
extension, it's quite possible that the ASPNET engine is not even
called, and the configuration file will never be used to map the URL.

Another way to pre-process URLs is to write a HttpModule, and to add it
in the chain of the ASP.NET request process. However, the same comment
applies, and I am not sure it will work for URLs which are not explicity
mapped to ASP.NET content.
http://msdn.microsoft.com/library/de...lrewriting.asp

Short of that, I guess that what's left to you is usig ISAPI filters...

Good luck,
Laurent
--
Laurent Bugnion, GalaSoft
Software engineering: http://www.galasoft-LB.ch
Private/Malaysia: http://mypage.bluewin.ch/lbugnion
Support children in Calcutta: http://www.calcutta-espoir.ch
Nov 9 '06 #6
Short of that, I guess that what's left to you is using ISAPI filters...

In between 'standard' remapping in the web.config and ISAPI it's also
possible to do this nicely using a custom error page. I was recently
working on a site doing something similar to the OP's original
requirements. Because it was shared hosting we could not install an
ISAPI extension and had to work around. The answer was as follows:

1) Create a custom error page (.ashx) and tell server to redirect all
404 errors to it.
2) Obtain the original request (with the service we were using it came
in as a url parameter on the request for the error404.ashx page).
3) Parse the original request into the target request using whatever
manner of processing suits. Unlike using web.config with this method
you can leverage regular expressions to allow more intelligent
processing and fewer mapping entries (e.g.
{ "^/dating/detail/(.*).htm","/pages/dating/detail.aspx?product=$1"})
4) Assuming a mapping is found, set the response status code to 200 and
use RewritePath, GetCompiledPageInstance and ProcessRequest to build
and serve back the page as required.
5) If the mapping was not found, return a simple 404 error page with
the original error code as the response status code.

This worked very neatly on this site (www.scourtheweb.info/dating)
where you will see all links have a .htm extension. We don't actually
use this extension for files on the site (we use .html for static
files) so we know every .htm requires remapping.

The only other change we had to introduce was a condition in our global
exception logging code to make it ignore the exceptions raised by IIS
when it first realised the requested .htm file didn't exist.

One other fringe benefit of this, if you need to temporarily stop
remapping for whatever reason, you can create a complete or partial set
of .htm site files and put them on the server. This can be handily
stored in a subfolder that gets renamed to switch the alternative
static site on and then back off during site changes.

Ratty Atkins
Zero D Limited

Nov 10 '06 #7
Hi,

Ratty wrote:
>Short of that, I guess that what's left to you is using ISAPI filters...

In between 'standard' remapping in the web.config and ISAPI it's also
possible to do this nicely using a custom error page. I was recently
working on a site doing something similar to the OP's original
requirements. Because it was shared hosting we could not install an
ISAPI extension and had to work around. The answer was as follows:
Cool idea. I now remember I saw that in another thread somewhere. Was it
from you?

Thanks for sharing

Greetings,
Laurent
--
Laurent Bugnion, GalaSoft
Software engineering: http://www.galasoft-LB.ch
Private/Malaysia: http://mypage.bluewin.ch/lbugnion
Support children in Calcutta: http://www.calcutta-espoir.ch
Nov 10 '06 #8
As for the text file, this does exactly what I want:

pageContent = File.ReadAllText(Server.MapPath(string.Concat(
@"pagecontent\", page, ".txt")));

lblPageContent.Text = pageContent;

Nov 11 '06 #9
Sadly no. I have to acknowledge that others posted articles about this
prior to us even hitting the problem. The only bits I think we can
claim as vaguely original are the idea of explicitely choosing a
particular extension for exclusive use in remapping and, perhaps, the
idea of using this to keep the site up and running during critical
updates. I should probably add that we generate the static site copy
from within a version of the remapping file that makes an appropriately
located and names disk copy of each page before it's sent back as the
response. This alternative remapping file is put in place and then we
do a broken link search which automatically generates a complete and up
to date static copy of the site. Incoming requests now automatically
hit the created 'htm' files so we are free to tinker with the rest of
the site until we are happy, at which point we delete/rename folder to
take down the statics.

Having made a tentative claim of originality I should also add that I
am sure lots of people can point to lots and lots of earlier examples
of this sort of thing elsewhere. We are, after all, software engineers
and are fully aware that we basically reinvent things for a living!
Laurent Bugnion wrote:
Hi,

Ratty wrote:
Short of that, I guess that what's left to you is using ISAPI filters...
In between 'standard' remapping in the web.config and ISAPI it's also
possible to do this nicely using a custom error page. I was recently
working on a site doing something similar to the OP's original
requirements. Because it was shared hosting we could not install an
ISAPI extension and had to work around. The answer was as follows:

Cool idea. I now remember I saw that in another thread somewhere. Was it
from you?

Thanks for sharing

Greetings,
Laurent
--
Laurent Bugnion, GalaSoft
Software engineering: http://www.galasoft-LB.ch
Private/Malaysia: http://mypage.bluewin.ch/lbugnion
Support children in Calcutta: http://www.calcutta-espoir.ch
Nov 11 '06 #10

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

Similar topics

6
by: Suresh Kumaran | last post by:
Hi All, Does anybody know the sytax in VB.NET to write the contents of a multiline text box to a text file? Appreciate help. Suresh
1
by: fabrice | last post by:
Hello, I've got trouble reading a text file (event viewer dump) by using the getline() function... After 200 - 300 lines that are read correctly, it suddenly stops reading the rest of the...
19
by: Lionel B | last post by:
Greetings, I need to read (unformatted text) from stdin up to EOF into a char buffer; of course I cannot allocate my buffer until I know how much text is available, and I do not know how much...
0
by: Eric Lilja | last post by:
Hello, I have a text file that contains a number of entries describing a recipe. Each entry consists of a number of strings. Here's an example file with only one entry (recipe): Name=Maple Quill...
1
by: Magnus | last post by:
allrite folks, got some questions here... 1) LAY-OUT OF REPORTS How is it possible to fundamentaly change the lay-out/form of a report in access? I dont really know it that "difficult", but...
50
by: Michael Mair | last post by:
Cheerio, I would appreciate opinions on the following: Given the task to read a _complete_ text file into a string: What is the "best" way to do it? Handling the buffer is not the problem...
2
by: Sabin Finateanu | last post by:
Hi I'm having problem reading a file from my program and I think it's from a procedure I'm using but I don't see where I'm going wrong. Here is the code: public bool AllowUsage() { ...
4
by: dale zhang | last post by:
Hi, I am trying to save and read an image from MS Access DB based on the following article: http://www.vbdotnetheaven.com/Code/Sept2003/2175.asp Right now, I saved images without any...
4
by: Amit Maheshwari | last post by:
I need to read text file having data either comma seperated or tab seperated or any custom seperator and convert into a DataSet in C# . I tried Microsoft Text Driver and Microsoft.Jet.OLEDB.4.0...
3
by: The Cool Giraffe | last post by:
Regarding the following code i have a problem. void read () { fstream file; ios::open_mode opMode = ios::in; file.open ("some.txt", opMode); char *ch = new char; vector <charv; while...
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.