Contents tagged with ASP.NET

  • Simple and Custom Validation in MVC 3

    This is the first of a series on validation and business rules in MVC 3. I'll start out simple and we'll work out way up to the more complex. I'm going to build on the solution created in the previous blog post (Delete Like a Rock Star) so go there and get set up. Continue reading...

  • Render HTML for Menu in MVC 3 Using SiteMapProvider

    There are several good jQuery menu plugins but I like the old skool ASP.NET SiteMapProvider because I can declare my app roles in there and get security trimming for free. Add and configure the default XmlSiteMapProvider or customize one to override the IsAccessibleToUser method. Your choice. Then add this MenuHelper static class to your project:  public static class MenuHelper { public static MvcHtmlString Menu(this HtmlHelper helper) { var sb = new StringBuilder();  sb.Append("<ul id='nav'>");  // Render each top level node var topLevelNodes = SiteMap.RootNode.ChildNodes; foreach (SiteMapNode node in topLevelNodes) { sb.AppendLine("<li>");  sb.AppendFormat("<a href='{0}'>{1}</a>", node.Url, helper.Encode(node.Title)); if (node.HasChildNodes) { sb.AppendLine("<ul>"); foreach (SiteMapNode childNode in node.ChildNodes) { sb.AppendLine("<li>"); sb.AppendFormat("<a href='{0}'>{1}</a>", childNode.Url, helper.Encode(childNode.Title)); sb.AppendLine("</li>"); } sb.AppendLine("</ul>"); }  sb.AppendLine("</li>"); }  // Close unordered list tag sb.Append("</ul>");  return new MvcHtmlString(sb.ToString()); } } If this is the first helper for the project then add the namespace where your MenuHelper class lives to your Views Web.config file: <system.web.webPages.razor> <host … Continue reading...

  • TruthWidget Launch Part 2

    TruthWidget.com is fully ported over to MVC 3 from Silverlight 4. I ran through all of my tests and so it's up and running live. The awesome part was being able to reuse Entity Framework 4 and the repository wrappers. I took the RIA web services and put them into MVC services. I've seen examples where the controller calls EF directly but that makes me uncomfortable so I wanted that additional layer of abstraction. The bulk of my work was throwing away the XAML and recreating Html (Razor) web pages. What I liked about my experience is how I can create HtmlHelpers (little helper methods that emit small chunks of HTML), partial views, and stitch them together to create the whole view. This enabled me to follow the DRY principle a lot more than in XAML where I tended to have big bloated views -- especially if I did it in Blend. I'm really happy with my experience. I'll blog on specific tips and tricks when I have time. For now I'll just say that everything just works in MVC 3. It's a real joy to work with it. Continue reading...

  • IIS 7 Errors

    On my sandbox (WinOS 2008) I installed Web Platform Installer 2.0. Then through the PI I installed Frameworks and Runtime (.NET Framework 4.0), IIS 7, and Web Deploy Tool 1.1. I took a vanilla ASP.NET app from the new VS2010 project template and deployed to IIS 7 using the new packaging tools. Depending upon whether the app pool was integrated pipeline or classic I got these errors: Continue reading...

  • Brad Abrams on RIA Services with EF

    Abrams basically walked through a typical RIA Services app and most of it is familiar to anyone who has studied the RIA Services documentation and walkthrough. However there were several new things specific to VS2010 and the new bits. Continue reading...

  • Microsoft PDC09 Keynote Address

    Microsoft announced the new bits at PDC keynote today. The theme? Cloud, cloud, and more cloud. The Azure platform goes into production on Jan 1. In beta through the next several months are new cloud platform tools: AppFabric (previously code named "Dublin") and Endpoint. From what I understand AppFabric provides a unified hosting environment for WCF and Workflow, SQL Azure database caching, and can be configured right in IIS. Endpoint seems to be a discovery service for web services all over the world. You can search it and get all the information you need to consume that data in your application. It probably uses the REST protocol. Microsoft showed a pretty cool demo with the NASA web service that involved showing 3D landscapes of pictures taken by the Mars Rover. You could do a serious mashup in a .NET app with Endpoint. Continue reading...

  • Visual Studio 2008: Unable to create the Web site

    After a brief foray into Windows 7 Beta just to check it out I took my laptop back to Vista 64-bit, wiping everything out in the process to get a clean slate. Many moons ago, when first installing Vista, it took me about 3 minutes before I disabled the UAC. This time I'm trying hard not to disable it. (You know, best practices about not developing as a sysadmin and all of that.) Now I'm running into security issues that I'm forced to address rather than bypass. In the end this is a good thing. Here's the latest: In VS2008 I right-clicked on the solution and chose Add > New Web Site. Then I picked the WCF Service template and changed the location to HTTP. This tells VS to create a new virtual application beneath the Default Web Site (in my case c:\inetpub\wwwroot) but I got the error: "Unable to creat the Web site 'http://localhost/Foo' because I was not running Visual Studio in an administrator context. The solution is to run VS as an administrator (you can create a shortcut of devenv.exe and under the Advanced tab toggle "Run as administrator") in order to add the web site. Then save and close and go back in under your logged in credentials. Continue reading...

  • STORM! ORM Mapper Beta Launch

    I'm happy to announce that SquareWidget in partnership with the good folks at FlishHorse have launched the Strongly-Typed Object Relational Mapper (STORM) Beta. Download it now and take it for a spin. What is STORM? It's an add-in into Visual Studio 2005. What does it do? It's an OR mapper. It's a code generator. And there are no third-party black box assemblies or other wackiness. We've used Subsonic, NHibernate, and many other ORM tools and we came to the conclusion that it's no fun writing and maintaining a bunch of XML scripts. And we also didn't like creating custom templates like in CodeSmith. We're lazy. So we made a tool that's trivial to use. Continue reading...

  • Fetching Connection Strings

    Let's assume you wish to store your database connection string in the <connectionStrings> configuration section of Web.config. This is the best practice in my opinion because you can (and should) encrypt the section in production. Now you have two basic options: Continue reading...

  • Enumerate Application Settings

    Here's my own solution to a problem that seems to plague a lot of .NET developers out there. The problem is how best to enumerate application settings stored in a section of an ASP.NET 2.0 app.config file at runtime. Let's suppose you have a settings file called Color.settings with the following key-value pairs: Continue reading...

  • Custom SiteMapProvider

    Usually custom SiteMap providers are written because we're pulling our nodes from a source other than xml. Jeff Prosise's provider written for SQL Server is a classic example. But I had a case recently in which the out of the box XmlSiteMapProvider was just fine; my problem was that I needed to do some peculiar security trimming on the nodes. No problem, just add roles to the nodes and enable security trimming in Web.config for the provider right? Not exactly. The requirements were that the system had to ship with the ability to turn a major piece of functionality on or off in production. Let's call the functionality "ViewWidgets" and let's say there's an app setting in Web.config called "ViewWidgets" with value="false". That means, the web site should suppress the Widgets unless and until it is flipped to true. Then Widgets should be available to the end user. One way of implementing this would have been to set a generic role or override Web.config in a subfolder. But I wanted the webmaster to be able to flip the flag to true or false in Web.config where he was more comfortable working. The solution turns out to be pretty easy. Just derive a custom provider from XmlSiteMapProvider and override the Initialize method to trim the node based on the app setting: Continue reading...