I just start playing with WebPages (Razor) and I figured out some basic concepts behind the wall. First of all I list my always-have-to things for each site I build:
- Friendly urls of course (WebPages do it for me)
- Translated urls (WebPages supports parameters so its easy to manage)
- Multiple template support (WebPages not support by default, some logic needed here)
- Separated code for each function to avoid duplicates (WebPages supports it behind the wall)
So if you think about above list the good old MVC pattern comes into play. ASP.NET already has an MVC(3) framework which provide all functionality I need. I know that well. At this question I try to figure out if WebPages technology would be also perfect platform to develop large-scale MVC (-like) web projects.
I tested the page load behaviour / chain of WebPages and figured out its really a chain built by loaded page and one or more nested layouts. Every layout page has a pointer to its loader (Parent) page.
At this point, I have to clear some basic concepts. Since WebPages resolve urls by physical files and folders (paths) translated urls indirectly supported because I can create translated folders and files by using paths like this: ..../en/account/register and I can create another path for example hungarian language path: .../hu/szemelyes/regisztracio.
All I need is to separate code because its not so elegant to write register logic into both .cshtml file. WebPages supports @helpers and @functions so its easy to create an "Account.cshtml" and create all function I need. Its the official answer to my question.
What happens behind the wall if I wrote a @function in some .cshtml (helper) file? Its creates a new class for me which inherits WebPageHelper. I think this not too elegant way since I can create my own class to provide same functionality.
Some further research I found that every page (and layout page) inherits WebPage class by default. With @inherits directive I can override default inheritance and its perfect way to create my own class derived from WebPage, and in all my .cshtml file I can directly inherit from that page.
At this point I can separate code (I think:) more elegant way than using @functions. What about text translations? I think its a general way to use resource files but with WebPages I think about to store text values in plain .cshtml files. What do you think about it?
Finally my register.cshtml:
@inherits Account.Register
@{
Title = "Please Register"
}
..and my regisztracio.cshtml:
@inherits Account.Register
@{
Title = "Kérem regisztráljon"
}
My Account.Register class has a String property Title:
public class Register : WebPage
{
public String Title { get; set; }
...
}
There is another thing I have to tell. My register.cshtml (as well as the regisztracio.cshtml) does not contain any html markup. I have a default layout file (_Master.cshtml) and I have another layout files for each page (_Register.cshtml) under my "View" directory. My code automatically load the correct page-layout file by name of my loaded "controller" class. So my Register class automatically load "_Register.cshtml" layout file. And all page-layout files automatically loads _Master.cshtml (or _Mobile.cshtml if visitor used mobile device) which is the main layout.
So I think this is an MVC pattern implemented for WebPages technology while keeps WebPages strong functionality like automatic url resolving.
What do you think?
So its my basic concept with WebPages