Sunday, October 30, 2011

MVC 3 Razor C# View Engine adding

As a OOD web developer, I like to group usercontrols, pages to related folders.
In MVC 3, Default Search view engine works like the following:

If user controls, the View engine finds under View folder OR Shared folder only.

Here is a problem:

If I created a folder under shared folder and your user control having in that folder, MVC ViewEngine couldn't find the correct user control via embedded usercontrol's page.

.Net throws this error.




But in MVC, every class that integreated in MVC you can extend as your custom requried.

Here is an example that I've found in internet that somebody shared in this website
(Waynehaffenden.com


RazorCSharpViewEngine.cs
view source

01 using System.Web.Mvc;
02 public class RazorCSharpViewEngine : RazorViewEngine
03 {
04 public RazorCSharpViewEngine()
05 {
06 AreaViewLocationFormats = new[]
07 {
08 "~/Areas/{2}/Views/{1}/{0}.cshtml",
09 "~/Areas/{2}/Views/Shared/{0}.cshtml"
10 };
11 AreaMasterLocationFormats = new[]
12 {
13 "~/Areas/{2}/Views/{1}/{0}.cshtml",
14 "~/Areas/{2}/Views/Shared/{0}.cshtml"
15 };
16 AreaPartialViewLocationFormats = new[]
17 {
18 "~/Areas/{2}/Views/{1}/{0}.cshtml",
19 "~/Areas/{2}/Views/Shared/{0}.cshtml"
20 };
21 ViewLocationFormats = new[]
22 {
23 "~/Views/{1}/{0}.cshtml",
24 "~/Views/Shared/{0}.cshtml"
25 };
26 MasterLocationFormats = new[]
27 {
28 "~/Views/{1}/{0}.cshtml",
29 "~/Views/Shared/{0}.cshtml"
30 };
31 PartialViewLocationFormats = new[]
32 {
33 "~/Views/{1}/{0}.cshtml",
34 "~/Views/Shared/{0}.cshtml"
35 };
36 }
37 }


Your Global.ascs's Application start() method should be as following.

protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();

RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);

ViewEngines.Engines.Add(new RazorCustomViewEngine());
}

Friday, October 7, 2011

OOD Project?

Today, one of my colleague ( as Senior developer), he told me his project is OOD programming project. I didn't say anything back to him. Just laughed myself.
Please look up following code whether it's OOD porject or not.

It's about binding Portfolio data from portfolio database.



I tired to add a new simple functionality to his code. And also he's always saying his code the best one.