First off... I really enjoy Web Matrix. I have already(very rapidly) created two locally host full fledged websites. These arent production ofcourse, but I was really impressed with how fast I was able to learn and build dynamic sites.
I want to learn more! I have some basic questions below;
Can I call functions from a .cs to a .cshtml? Explained: Could I take an object oriented approach to RAZOR pages? If I were to create a class with some methods/functions/objects could I use them from my .cshtml?
Does anyone have an example of this?
Can I create functions inside a .cshtml page? Explained: Is this different from classic asp syntax?
Should I be looking at classic asp syntax to learn more?
If I wanted to create my own "helper" functions and/or add to the RAZOR api - is this something that is open? Explained: I'd love to be able to create my own set of "things" that I can continue to use in further projects?
Yes? No?
There are no clear examples or clear case usage of the html GET capability. Again, should I consider classic asp syntax for this?
What will RAZOR be like down the road? Is there a goal?
You have two different choices. Either you can use querystrings and get the values with Request.Querystring["name"], or you can use routing and get the values with UrlData[index].
Example with routing:
mysite.com/index/Home/05032010
And in index.cshtml:
var page = UrlData[0];
var date = UrlData[1];
You don´t have to change anything to use routes. It will automatically look after index.cshtml.
Mikael Söderström
http://weblogs.asp.net/mikaelsoderstrom
http://www.twitter.com/vimpyboy
Marked as answer by Mikesdotnetting on Jul 10, 2010 02:07 PM
Looks like everyone got your questions answered. Great! Just wanted to throw out one cool thing that we're working on for the next release. In addition to the @functions keyword, you'll be able to create your own real helpers in the future using Razor.
Here's how it will work:
Say you have a page that has a little markup that you want to reuse on other pages (I'll use a Gravatar image as a simple example, but it could be any markup or code)...
does razor support "if"? I'm still looking at the api guide for references to all programming terms... @if(){ } else { } is that right? Is there an updated guide?
Erik5388
Member
81 Points
84 Posts
Extending RAZOR (functions, classic asp, .cs)
Jul 08, 2010 08:28 PM|LINK
Hi all,
First off... I really enjoy Web Matrix. I have already(very rapidly) created two locally host full fledged websites. These arent production ofcourse, but I was really impressed with how fast I was able to learn and build dynamic sites.
I want to learn more! I have some basic questions below;
Explained: Could I take an object oriented approach to RAZOR pages? If I were to create a class with some methods/functions/objects could I use them from my .cshtml?
Does anyone have an example of this?
Explained: Is this different from classic asp syntax?
Should I be looking at classic asp syntax to learn more?
Explained: I'd love to be able to create my own set of "things" that I can continue to use in further projects?
Yes? No?
.cshtml .cs class functions classic asp
zettersten.com
client side dev
canvas/html5/js/css
Mikesdotnett...
All-Star
154818 Points
19853 Posts
Moderator
MVP
Re: Extending RAZOR (functions, classic asp, .cs)
Jul 08, 2010 10:15 PM|LINK
Yes. You can create a normal class file with methods, and put it into an App_Code folder (which you will need to add).
Yes. There is a Razor syntax for those:
@functions {
int Add(int a, int b)
{
return a + b;
}
}
See above
Could you explain what you are after?
Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter
Erik5388
Member
81 Points
84 Posts
Re: Extending RAZOR (functions, classic asp, .cs)
Jul 08, 2010 10:35 PM|LINK
Thanks for the quick answers.
Like:
mysite.com/index.cshtml?page=Home&date=05032010
Can I grab "Home" and "05032010" and store them into a RAZOR var?
zettersten.com
client side dev
canvas/html5/js/css
Vimpyboy
Contributor
3212 Points
651 Posts
MVP
Re: Extending RAZOR (functions, classic asp, .cs)
Jul 08, 2010 10:40 PM|LINK
You have two different choices. Either you can use querystrings and get the values with Request.Querystring["name"], or you can use routing and get the values with UrlData[index].
Example with routing:
mysite.com/index/Home/05032010
And in index.cshtml:
You don´t have to change anything to use routes. It will automatically look after index.cshtml.
http://weblogs.asp.net/mikaelsoderstrom
http://www.twitter.com/vimpyboy
Erik5388
Member
81 Points
84 Posts
Re: Extending RAZOR (functions, classic asp, .cs)
Jul 08, 2010 11:20 PM|LINK
I see that now in the documentation! Thanks vimpy. My head was on backwards.
zettersten.com
client side dev
canvas/html5/js/css
HumanCompile...
Member
511 Points
100 Posts
Microsoft
Re: Extending RAZOR (functions, classic asp, .cs)
Jul 09, 2010 06:25 PM|LINK
Looks like everyone got your questions answered. Great! Just wanted to throw out one cool thing that we're working on for the next release. In addition to the @functions keyword, you'll be able to create your own real helpers in the future using Razor. Here's how it will work:
Say you have a page that has a little markup that you want to reuse on other pages (I'll use a Gravatar image as a simple example, but it could be any markup or code)...
<img src="@Gravatar.GetUrl("someone@somewhere.com")" alt="Gravatar" />
Put a file under App_Code (maybe call it, MyHelpers.cshtml) and add this to it....
@helper GetGravatarImage(string email) {
}
Now cut/paste the html you want to reuse into that new file in the newly created method...
@helper GetGravatarImage(string email) {
<img src="@Gravatar.GetUrl("someone@somewhere.com")" alt="Gravatar" />
}
Switch out the static email address for the variable and you're done...
@helper GetGravatarImage(string email) {
<img src="@Gravatar.GetUrl(email)" alt="Gravatar" />
}
Now in any page you can call it...
@MyHelpers.GetGravatarImage("someelse@somewhereelse.com")
Again, this isn't in the beta, but will be in the next version. Just a little preview. We'll continue to make things easier. :)
ASP.NET PM
http://about.me/erikporter
Erik5388
Member
81 Points
84 Posts
Re: Extending RAZOR (functions, classic asp, .cs)
Jul 09, 2010 07:53 PM|LINK
Now that was actually my original question. "Extending RAZOR". This is where I can see people taking it to the next level.
Unother huge question: is there a more detail/updated area where I can reference the API?
The current one doesnt show @function and I am wondering whatelse Im missing.
zettersten.com
client side dev
canvas/html5/js/css
HumanCompile...
Member
511 Points
100 Posts
Microsoft
Re: Extending RAZOR (functions, classic asp, .cs)
Jul 09, 2010 08:35 PM|LINK
This is the most updated reference.
http://www.asp.net/webmatrix/tutorials/asp-net-web-pages-api-reference
I will go see if we can get @functions added. Thanks for catching that!
ASP.NET PM
http://about.me/erikporter
Erik5388
Member
81 Points
84 Posts
Re: Extending RAZOR (functions, classic asp, .cs)
Jul 15, 2010 06:34 PM|LINK
zettersten.com
client side dev
canvas/html5/js/css
HumanCompile...
Member
511 Points
100 Posts
Microsoft
Re: Extending RAZOR (functions, classic asp, .cs)
Jul 15, 2010 07:59 PM|LINK
Everything that is interpreted as code (either between @{ } or after @) is just interpreted as C#. So yes, if works.
@if (something) { } else { }ASP.NET PM
http://about.me/erikporter