Reading from text file?

B

brett

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
 
L

Laurent Bugnion

brett said:
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
 
B

brett

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
 
L

Laurent Bugnion

Hi Brett,
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
 
B

brett

2.) How can I get rid of the query string so that the url looks like
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
 
L

Laurent Bugnion

Hi,
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/default.asp?url=/library/en-us/dnaspp/html/urlrewriting.asp

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

Good luck,
Laurent
 
R

Ratty

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
 
L

Laurent Bugnion

Hi,
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
 
B

brett

As for the text file, this does exactly what I want:

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

lblPageContent.Text = pageContent;
 
R

Ratty

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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,586
Members
45,089
Latest member
Ketologenic

Latest Threads

Top