p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/)
-   BOOK: Beginning ASP.NET Web Pages with WebMatrix (http://p2p.wrox.com/book-beginning-asp-net-web-pages-webmatrix-662/)
-   -   Chapter 4: Reusable Functions (http://p2p.wrox.com/book-beginning-asp-net-web-pages-webmatrix/86524-chapter-4-reusable-functions.html)

spasticjustice February 1st, 2012 04:10 PM

Chapter 4: Reusable Functions
 
I'm having some difficulties getting this to run properly. IIS says there's a compilation error on line 10. I also couldn't run the code properly with a folder named App_Data. IIS gives me a 404.8 error.

Here's the code:

Code:

<!DOCTYPE html>

<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title></title>
    </head>
    <body>
        mike@wrox.com is a valid email address:
        @Functions.IsValidEmail("mike@wrox.com")<br />
        www.wrox.com is a valid email address:
        @Functions.IsValidEmail("www.wrox.com")<br />
    </body>
</html>

Code:

@using System.Text.RegularExpressions;

@functions {
public bool IsValidEmail(string s)
{
    string pattern = @"^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$";
    return Regex.IsMatch(s, pattern);
}
}


Imar February 2nd, 2012 01:45 PM

Hi there,

You declare functions with a lower case f, but when you call it you use a capital F. Since C# is case sensitive, this won't work.

You can't serve pages from the App_Data folder. For a detailed explanation, take a look at this post: http://p2p.wrox.com/book-beginning-a...e-404-8-a.html

Cheers,

Imar

spasticjustice February 2nd, 2012 11:29 PM

Secondary issue
 
Thanks Imar. Now I'm having a secondary issue. After I made those corrections (this should be in the eratta btw because this is straight from the book), I am now recieving a compilation error with this portion of code.

Code:

<!DOCTYPE html>

<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title></title>
        @Helpers.RegisterJs("/Script", new[] {"myScript.js"})
        @Helpers.RegisterStyles("/Content", new[] {"Main.css"})
    </head>
    <body>
        mike@wrox.com is a valid email address:
        @functions.IsValidEmail("mike@wrox.com")<br />
        www.wrox.com is a valid email address:
        @functions.IsValidEmail("www.wrox.com")<br />
    </body>
</html>

Code:

@helper RegisterJs(string folder, string[] scriptLibs)
{
    foreach (var script in scriptLibs){
        <script src="@folder/@script" type="text/javascript"></script>
    }
}
@helper RegisterStyles (string folder, string[] styleSheets)
{
    foreach (var sheet in styleSheets){
        <link href="@folder/@sheet" rel="stylesheet" type="text/css" />   
    }
}

I've tried helpers, helper, Helper, and helpers with no success. It's also worth noting that App_Code is straight from the book, I mistyped App_Data. It's my understanding that App_Code is also off limits. I'm getting the same error as if I had used App_Data. Since then I've renamed the folder Test_Code and this circumvents this issue.

Mike Brind February 3rd, 2012 02:25 AM

Your IsValidEmail method is missing the static keyword in its definition:

public static bool IsValidEmail(string s)

The file should be placed in App_Code, and the method should be invoked using the file name without the extension. This is explained in the last paragraph of the How It Works section that relates to creating your reusable function. CSHTML files placed in App_Code are compiled to C# classes and the class takes the name of the file minus the extension.

If you called the file "functions.cshtml", you call the method like this:

@functions.IsValidEmail("mike@wrox.com")

If you named the file with a capital "F" (as described in Step 2), then you call the method like this:

@Functions.IsValidEmail("mike@wrox.com")

spasticjustice February 3rd, 2012 12:54 PM

I've tried it both ways
 
The code started out with a capital F, as well as the static keyword. I was attempting to troubleshoot it on my own after the initial way did not work. Per your suggestion, I've returned the code to it's original state as follows.

Code:

@using System.Text.RegularExpressions;

@functions {
public static bool IsValidEmail(string s)
{
    string pattern = @"^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$";
    return Regex.IsMatch(s, pattern);
}
}

Code:

@helper RegisterJs(string folder, string[] scriptLibs)
{
    foreach (var script in scriptLibs){
        <script src="@folder/@script" type="text/javascript"></script>
    }
}
@helper RegisterStyles (string folder, string[] styleSheets)
{
    foreach (var sheet in styleSheets){
        <link href="@folder/@sheet" rel="stylesheet" type="text/css" />   
    }
}

and lastly
Code:

<!DOCTYPE html>

<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title></title>
        @Helpers.RegisterJs("/Script", new[] {"myScript.js"})
        @Helpers.RegisterStyles("/Content", new[] {"Main.css"})
    </head>
    <body>
        mike@wrox.com is a valid email address:
        @Functions.IsValidEmail("mike@wrox.com")<br />
        www.wrox.com is a valid email address:
        @Functions.IsValidEmail("www.wrox.com")<br />
    </body>
</html>

There's still a compilation error, "Helpers does not exist in this current context". The name of the file is Helpers.cshtml, and the name of the other is Functions.cshtml. If I leave the folder as App_Code I get this error, "HTTP Error 404.8 - Not Found
The request filtering module is configured to deny a path in the URL that contains a hiddenSegment section."

Mike Brind February 3rd, 2012 01:24 PM

Put the helpers and functions files back in App_Code. Make sure that the other page IS NOT in App_Code. When you try to launch the page that contains the calls to the functions, make sure it is selected in the file explorer. You cannot launch pages from App_Code. That causes the 404 error.

spasticjustice February 3rd, 2012 02:36 PM

I'm very grateful
 
Working like a charm! Thanks so much for helping me sort this one out! Now, I can move forward with this awesome book.


All times are GMT -4. The time now is 05:51 AM.

Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
2013 John Wiley & Sons, Inc.