Recently I’ve been working with some of the newer IIS7/7.5 and using the IIS Manager, which happens to be quit nice. They’ve done a great job at simplifying management from a IT perspective as well as a development perspective. I ran into some trouble while trying to use the PUT and DELETE verbs in a ASP.NET MVC site. I’m a bit of a purist, to some extent, and like to use the verbs for what they mean. I kept getting a request not accepted using the PUT and DELETE verbs. It turns out that you need to remove the WebDAV from the HttpHandlers and HttpModules which can easily be found in the IIS manager. Below shows a screen shot of the management view and the 2 location where you need to remove the WebDAV to enable PUT and DELETE methods.
Deploy ASP.NET MVC Dependencies to Hosting Provider
Lately I’ve been working with Visual Studio 2010 and have been quite impressed by some of it’s features. One feature in particular is the web site publishing capabilities. The other day I was trying to frantically get a school project done and needed to deploy to my hosting provider, WinHost. I have to briefly say that I’ve worked with a few top name providers that host ASP.NET and this one has been the best with great how-to’s on up-to-date technologies like Visual Studio Web Deploy and has a awesome management console. Anyway, that’s not what I came to write about. So after I deploy my code with a snap using Web Deploy, I get a few errors when trying to run my site. Here I was all stoked that the Web Deploy was so effortless, to come to message like “Could not load file or assembly System.Web.WebPages.Razor” and “Could not load file or assembly System.Web.Helpers”. Man what a buzz kill. So not all hosting providers have ASP.NET MVC 3 yet (i.e. loaded in the GAC on there servers) which obviously we can’t do. There are a couple of ways to fix this. One is to grab the dlls listed below and store them in some library folder from which you can reference in your project. The alternative, and better approach, is to indicate that your references you already have should be set to “Copy local”. I’ve provided a screen shot and a list off the dlls which are needed by a ASP.NET MVC 3 application. If you’re doing a MVC 2 or you will not need all these dlls.
DLLs needed:
- Microsoft.Web.Infrastructure
- System.Web.Abstractions
- System.Web.Helpers
- System.Web.Mvc
- System.Web.Razor
- System.Web.Routing
- System.Web.WebPages
- System.Web.WebPages.Razor
You may be wondering why your project doesn’t have all these references. No sweat. You can just add them. I like to install Visual Studio Productivity Power Tools, it makes the add reference experience nicer. Once you have the DLLs referenced in you project, right click the ones listed above and select Properties. Set the “Copy local” attribute to true, see image below. That’s it, redeploy your site and you’re set!
Printable Web Page
Ever wonder how to make your unnecessary page elements disappear only to leave a perfectly printable web page? Using the stylesheet link’s media attribute can do the trick. Below describes how to utilize this media stylesheet to hide specific elements within your page. One of the keys to remember is that when your presenting mostly readable text, you want to stick to black and white with possibly red or blue links to let the user know certain text represented hyperlinks.
1. First thing to do is to define a css file to hold or media printing specific css. Let’s call it pring.css.
2. Add the following reference to the head section of your html document.
<link href="/Styles/Print.css" type="text/css" media="print" rel="stylesheet" />
3. Define the core css classed that drive most of you page. Below shows the core css classes we will use. The class “nonprintable” will be applied to any element that should not be shown on the printed page. Sometimes you will need to force certain css styles so you can use a “printable” class for this purpose. Applying black and white to the body of the html document is the cleanest way to obtain a slick page. Notice that “a” links get a red color and a transparent background; this helps the user realize the text was a hyperlink and prevent any a link’s background images from showing up.
/* GENERAL */
.nonprintable
{
display:none;
}
/* apply to specific items that must be forced */
.printable
{
background:white !important;
color:Black !important;
}
body
{
background:white;
color:Black;
}
/* A links */
a:link, a:visited
{
color:Red;
background: transparent;
font-weight: bold;
text-decoration: underline;
}
/* END GENERAL */
/*
asp.net ajax tab control
*/
.ajax__tab_header, .ajax__tab_outer, .ajax__tab_inner
{
display:none;
visibility:hidden;
}
.ajax__tab_body
{
border-color:White;
visibility:hidden;
}
5. Now apply css to elements within your document. Menu’s, images, ads, and other content that you don’t want to print should get the “nonprintable” class. This class will be assigned to the element but will not take affect until the user goes to print the page. Try it by applying some css and doing a print preview. This is a clever way of verify that the page looks correct without actually printing.
ASP.NET MVC Razor View Engine and Resources
The Razor view engine shipped with ASP.NET MVC 3 is very clean and has some nice coding enhancements, see a introduction on Scott Gu’s blog. As I was converting a application over to use the new view engine, I was stumped to find that the global/local resource method was not directly accessible as it was with the default view engine. In order to access a resource you would have to type the following expression ViewContext.HttpContext.GetGlobalResourceObject(classKey, resourceKey) which is quite length. To mitigate this, I create some common extensions to clean up the code. Below shows a clean way of doing this.
public static class CommonHtmlExtensions
{
public static object GetGlobalResource(this HtmlHelper htmlHelper, string classKey, string resourceKey)
{
return htmlHelper.ViewContext.HttpContext.GetGlobalResourceObject(classKey, resourceKey);
}
public static object GetGlobalResource(this HtmlHelper htmlHelper, string classKey, string resourceKey, CultureInfo culture)
{
return htmlHelper.ViewContext.HttpContext.GetGlobalResourceObject(classKey, resourceKey, culture);
}
public static object GetLocalResource(this HtmlHelper htmlHelper, string classKey, string resourceKey)
{
return htmlHelper.ViewContext.HttpContext.GetLocalResourceObject(classKey, resourceKey);
}
public static object GetLocalResource(this HtmlHelper htmlHelper, string classKey, string resourceKey, CultureInfo culture)
{
return htmlHelper.ViewContext.HttpContext.GetLocalResourceObject(classKey, resourceKey, culture);
}
}
@Html.GetGlobalResource("CommonText", "Some_ResourceKey")