views:

66

answers:

3

I found a good way to check if a file exists and read the contents if it does, but for some reason I can't create a method out of it.

Here's what I have so far:

<script runat="server">
    void Page_Load(Object s, EventArgs e) {

        lblFunction.Text = mwbInclude("test.txt");
    }


    string mwbInclude(string fileName) {
        string inc = Server.MapPath("/extra/include/"  + Request["game"] +  "/" + fileName);
        string valinc;

        if(System.IO.File.Exists(inc))
        {
            valinc = System.IO.File.ReadAllText(inc);
        }

        return valinc;
    }
</script> 

I wish I could provide more info, but the server this is on doesn't show any feedback on errors, just a 404 page.

+1  A: 

Why are you setting the Text property and calling Response.Write? Do you want to render the text as a label, or as the whole response?

Jon Skeet
Ah yes, of course, the Response.Write shouldn't be there. But even without it, it still fails.
skerit
+1  A: 

I think

valinc = Response.Write(System.IO.File.ReadAllText(inc));

should be

valinc = System.IO.File.ReadAllText(inc);
RedFilter
I overlooked that, indeed. But it still fails without it.
skerit
Please provide more info re "fails".
RedFilter
I wish I could, but it's a very closed system at work.And I have to do this directly on production, it doesn't spew errors.However, I "fixed" it by not using the function, but repeating the code each time. Not very neat, but oh well.
skerit
+1  A: 

If you're getting a 404, it's because your page isn't being found, not because there's a problem with the script itself. Have you tried ripping out all of the code and just sticking in some HTML tags as a sanity check?

Isaac Cambron