Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old February 1st, 2012, 05:10 PM
Registered User
Points: 15, Level: 1
Points: 15, Level: 1 Points: 15, Level: 1 Points: 15, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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);
}
}
Reply With Quote
  #2 (permalink)  
Old February 2nd, 2012, 02:45 PM
Imar's Avatar
Wrox Author
Points: 72,022, Level: 100
Points: 72,022, Level: 100 Points: 72,022, Level: 100 Points: 72,022, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,076
Thanks: 80
Thanked 1,587 Times in 1,563 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!
Reply With Quote
  #3 (permalink)  
Old February 3rd, 2012, 12:29 AM
Registered User
Points: 15, Level: 1
Points: 15, Level: 1 Points: 15, Level: 1 Points: 15, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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.
Reply With Quote
  #4 (permalink)  
Old February 3rd, 2012, 03:25 AM
Wrox Author
Points: 412, Level: 7
Points: 412, Level: 7 Points: 412, Level: 7 Points: 412, Level: 7
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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")
Reply With Quote
  #5 (permalink)  
Old February 3rd, 2012, 01:54 PM
Registered User
Points: 15, Level: 1
Points: 15, Level: 1 Points: 15, Level: 1 Points: 15, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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..
Reply With Quote
  #6 (permalink)  
Old February 3rd, 2012, 02:24 PM
Wrox Author
Points: 412, Level: 7
Points: 412, Level: 7 Points: 412, Level: 7 Points: 412, Level: 7
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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.
Reply With Quote
  #7 (permalink)  
Old February 3rd, 2012, 03:36 PM
Registered User
Points: 15, Level: 1
Points: 15, Level: 1 Points: 15, Level: 1 Points: 15, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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.
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 04:15 PM.


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