in

DĂ© specialist in .NET trainingen en consultancy

Jo-wen Mei

oktober 2008 - Posts

  • Dynamic Data points of interest

    Ok, when you've selected the right template to start off with Dynamic Data, there are some other issues you need to be aware of.

    1. The first time I tried to edit an entity in my DD site, I got an exception:

    Sys.webforms.pageRequestManagerServerErrorException: an unknown error occured while processing the request on the server.


    If you have been working with Ajax, this already may sound familiar... The reason of this exception being thrown is that partial rendering is enabled by default in DD. It makes the postbacks feel smoother, but also hides the real error. So, for debugging purposes, set EnablePartialRendering to false.

     

    2. Ok, after turning partial rendering off, the actual error appeared:

    A potentially dangerous Request.Form value was detected from the client...

    The new value contained a "<", which could indicate some code tag, and therefore is considered to be dangerous.

    Luckily, in my scenario, all the content editors know what they're doing. So I chose the lazy solution: turn form validation off!

    You can achieve this with the following config-setting:
    <pages validateRequest="false" />

    If anyone has a better solution, I'd like to hear it!

     

    3. You definitely don't want others to modify your website contents, so you need some kind of authentication.

    Here are some articles about an attribute based permission solution using user roles.

     

    4. Dynamic Data doesn't have any support for many-to-many (m:n) EF relationships out of the box.

    Someone wrote some templates that use reflection to achieve this [haven't tested this myself yet]

  • The location of the file or directory xxx is not trusted

    Today, I tried to run my unit-tests as I always do, but all of a sudden I received an error:

    The location of the file or directory "moq.dll" is not trusted

    where the hell does this come from? I can't remember having done anything special...

     

    anyway, I solved it by "unblocking" the file in Vista. Vista tries to recognize harmful files (that come from another computer, through email, messenger, etc) and blocks them by default. more info

     

    the other part of the solution is to tell .Net that the path where that dll lives, can be trusted (using caspol).

    In Vista, they don't have the .Net Framework Configuration installed by default. They moved it into a SDK.

    If you don't want to install this SDK, run the following command:

    caspol -machine -addgroup 1 -url file://<path>/* FullTrust -name FileW

     

    When you're running XP, try this:

    1. In the .NET Framework 2.0 Configuration, Go to Runtime Security Policy | Machine | All_Code
    2. Right click All_Code, select "New...", and select any name for your new group. Click Next
    3. Select URL as your condition
    4. Type \\machine_name\shared_folder\assembly.dll or \\machine_name\shared_folder\* and click Next
    5. Make sure permission is set to FullTrust
    6. Click Next, and Finish
    7. Close all your VS IDEs, restart, and try again

     

    Ok, back to focusing on the "real" problems..

    Posted okt 17 2008, 11:47 by Jo-wen with no comments
    Filed under:
  • MetadataType attribute

    I like the usage of attributes, mostly because it keeps my code clean. At the moment, I'm working with the Entity Framework, so my entities are generated. I want to use attributes for validation, and configure scaffolding (for DynamicData).

    But how can you place an attribute on a property of a generated class??

    If you put it in the generated class, it will be lost when you regenerate, obviously.

    Normally, you put the custom logic in the partial class, but it's just impossible to redefine properties as they're not partial.

    This is a common scenario, so there must be a solution to this: MetadataType attribute!

    image

    So the trick is: on the generated class, refer to a metadata class using the MetadataType attribute. Here, you can put all the attributes you want!

    (note: no instances of the metadata class are made)

     

    more info

    dynamic metadata

  • Dynamic Data: choose the right template

    I wanted to add Dynamic Data functionality in my existing mvc app. Therefore, I downloaded the sample from codeplex.

    The sample ran just fine, until I merged it with my mvc app. I received the following error:

    "The method 'Skip' is only supported for sorted input in LINQ to Entities"

    It's good to know there are 2 different templates:

    1. Dynamic Data Web Application (intended to work with LINQ to SQL)
    2. Dynamic Data Web Entities Application (intended to work with Entity Framework)

    and apparently, the sample was the wrong kind of template. So, select your template carefully!

     

    note1: if you're working with both of these technologies, check out this workaround

    note2: Differences When Working with the ADO.NET Entity Framework to LINQ to SQL

    note3: I was able to get the first type working with an entity model. (the registration of the model (either LINQ or EF) is the same for both templates); unfortunately, I couldn't find the required steps to switch from the first to the second template. Anyone?

  • PowerCommands vs Asp.Net MVC

    Sinds een paar dagen kan ik geen views meer openen in mijn mvc project. Visual Studio 2008 sluit direct zonder een of andere melding.

    Ik zie alleen een melding terugkomen in de errorlog:

    .NET Runtime version 2.0.50727.3053 - Fatal Execution Engine Error (6A2F5E00) (80131506)

    Het enige spannende wat ik had gedaan was de installatie van PowerCommands. Hiermee krijg je allerlei handige opties extra tot je beschikking in visual studio.

    Blijkbaar clashen deze twee met elkaar, want na deinstallatie van PowerCommands kan ik weer mijn aspx pagina's openen.

     

    PowerCommands vs Asp.Net MVC : 0 - 1

  • Enums in EF v1

    De eerste versie van het entity framework ondersteunt nog geen gebruik van enums.

    het is dus (nog) niet mogelijk om de int die je opslaat in je database, automatisch te converteren naar een System.Enum.

    Dit is echter bijzonder eenvoudig op te lossen: maak de interne enum (dus de int die je opslaat in je store) private, en definieer in de partial class een public property van het type System.Enum!

    info

  • De namespace wijzigen in EF

    Standaard wordt de naam van de edmx file gebruikt als namespace voor de gegenereerde classes. Dit kun je natuurlijk aanpassen, maar je moet wel weten waar.

    Als je de .edmx file opent, krijg je een mooie designer waar je allerlei dingen kan aanpassen. Zo is er een mooie "namespace" property die je kan instellen als je het conceptuele model selecteert (1e child in model browser), maar....

    als ik in de designer.cs kijk wat er verandert, zie ik dat alleen bepaalde attributen worden gewijzigd.

    Om de daadwerkelijke namespace van de gegenereerde classes te wijzigen moet je de properties bekijken van de .edmx file zelf!

    Hier kan je de gewenste namespace invullen bij de "custom tool namespace". Logisch!

    info

  • Asp.Net MVC deployen op IIS6

    Ok, het aan de praat krijgen van een mvc project op iis6 is geen pretje. Vandaar deze post, wellicht kan ik jullie wat ellende besparen.

    Ik moest de website op een 2003 server installeren.

    Zorg dat de volgende mvc assemblies terecht komen in de bin directory (copy local = true)

      • system.web.abstractions
      • system.web.routing
      • system.web.mvc

    zorg ook dat alle verwijzingen in je code naar resources door mvc worden berekent, met bijvoorbeeld ResolveUrl(). Zodra je de site test op een andere machine, kom je erachter dat je (ik iig) toch een paar links bent vergeten, zoals stylesheets of images.

     

    Het grootste probleem met IIS6 is dat deze niet de clean url's ondersteunt wat mvc zo krachtig maakt.

    Product/Details/5   wordt dan  Product.mvc/Details/5

    IIS6 verwacht namelijk altijd een extensie. Ik heb de mapping van de routes dan ook aangepast zodat deze .mvc bevat:

    routes.MapRoute(
                   "Default.mvc",                                          // Route name
                   "{controller}.mvc/{action}/{id}",                       // URL with parameters
                   new { controller = "Home", action = "Index", id = "" }, // Parameter defaults
                   new { controller = @"[^\.]*" }                          // Parameter constraints - Do not allow dots in the controller name
               );

    In de startpagina default.aspx heb ik in de code behind weer een redirect met .mvc staan:    

    Response.Redirect("~/Home.mvc/Index");

    Op de server moet je nog instellen dat .mvc extensies worden afgehandeld door de aspnet_isapi filter.

    Er is ook een andere mogelijkheid waarbij je wel de clean url's houdt, namelijk door alles te laten afhandelen door deze isapi filter. Het nadeel is wel dat dit (aanzienlijk) ten koste gaat van de performance.

    Er is ook nog een oplossing die gebruik maakt van best of both worlds: ISAPI_rewrite. Dit stukje software vangt de binnenkomende requests (clean url's), en stuurt deze door naar IIS met de mvc extensie. En zonder nadelige gevolgen voor de performance. Het is wel de lastigste oplossing om te implementeren.

     

    meer info:

    deploying asp.net mvc to iis6

    How to enable pretty urls with Asp.Net MVC and IIS6

    using asp.net mvc on iis6 without the mvc extension

    uitleg over url routing

    using asp.net mvc with different versions of iis (pdf)