Wrox Programmer Forums
|
BOOK: Beginning ASP.NET Web Pages with WebMatrix
This is the forum to discuss the Wrox book Beginning ASP.NET Web Pages with WebMatrix by Mike Brind, Imar Spaanjaars ; ISBN: 978-1-1180-5048-4
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET Web Pages with WebMatrix section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 1st, 2012, 05:10 PM
Registered User
 
Join Date: Feb 2012
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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);
}
}
 
Old February 2nd, 2012, 02:45 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

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: Chap 1 - Error Message 404.8

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old February 3rd, 2012, 12:29 AM
Registered User
 
Join Date: Feb 2012
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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.
 
Old February 3rd, 2012, 03:25 AM
Wrox Author
 
Join Date: Dec 2011
Posts: 57
Thanks: 1
Thanked 19 Times in 19 Posts
Default

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")
 
Old February 3rd, 2012, 01:54 PM
Registered User
 
Join Date: Feb 2012
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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."

Last edited by spasticjustice; February 3rd, 2012 at 01:58 PM..
 
Old February 3rd, 2012, 02:24 PM
Wrox Author
 
Join Date: Dec 2011
Posts: 57
Thanks: 1
Thanked 19 Times in 19 Posts
Default

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.
 
Old February 3rd, 2012, 03:36 PM
Registered User
 
Join Date: Feb 2012
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter 4:Reusable function hozdaman BOOK: Beginning ASP.NET Web Pages with WebMatrix 1 December 22nd, 2011 05:22 AM
Functions, Chapter 3 Building a Method bodycode BOOK: Beginning Microsoft Visual Basic 2008 ISBN: 978-0-470-19134-7 0 November 25th, 2009 06:23 PM
Chapter 2 functions mobyme BOOK: Beginning PHP5, Apache, and MySQL Web Development ISBN: 978-0-7645-7966-0 1 April 3rd, 2007 07:57 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.