0

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

thevan
  • 10,052
  • 53
  • 137
  • 202
ggabor
  • 1,642
  • 4
  • 18
  • 23

1 Answers1

0

I'm sure as you get more exposure to the framework you'll incorporate other practices. As to the language translation, you can use culture settings, together with resource files. You could set the current culture settings via global.asax, based on a query param.

Example of initializing the culture settings:

CultureInfo ci = new CultureInfo("en-US");
Thread.CurrentThread.CurrentCulture = ci;
Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture(ci.Name);

Then your solution would seamlessly make use of resource strings instead of literal strings, making your code cleaner and strongly typed. In your case it would also avoid having two separate registration views.

@inherits Account.Register
@{
    Title = StringsResource.RegisterTitle
}

Depending on the culture setting, the Title variable would hold either the "Please Register" text or the "Kérem regisztráljon".

Good luck!

Igorrious
  • 1,608
  • 16
  • 15
  • Yes, if I would use traditional resource files I don't need two or more language specific page file for translations. But I still need that two or more page files for url translations. If I keep only one page file and use cultureinfo and resource files how can I translate urls? WebPages already has a path base url routing so I think it would be easy to build my concept on it for url translations. Then I still need separated page files for each language. Then these files would be suitable to store language specific texts. – ggabor May 23 '12 at 05:53
  • You can create routes that will forward users to the same view, yet have different URLs. Looking at your code, your have a controller called account and view called register. Default route can be the same. To access the route in Hungarian you would have something like this: `routes.MapRoute("RegisterAccountHungarian", "szemelyes/regisztracio/{id}", new { controller = "account", action = "register", id = UrlParameter.Optional } );` You will need to add the language param. – Igorrious May 23 '12 at 17:31
  • And if I would do that I have to create MapRoute for each translated folder and file. With WebPages it's already done by default routing engine based on paths. I know how MVC3 works. Here I try to build a new concept expressly for WebPages framework. So I don't want to use MapRoute function. I try to build my solution based on simple file paths - folders and files. What I looking for is the best, ideal approach for this WebPages technology. Not for MVC3 framework. – ggabor May 24 '12 at 06:09