WebMatrix Beta 2
WebMatrix is package of tools you need to build Web sites using Windows. It includes IIS Developer Express (a development Web server), ASP.NET (a Web framework), and SQL Server Compact (an embedded database). It streamlines Web site development and makes it easy to start Web sites from popular open-source apps.
You can get more details about WebMatrix from Microsoft’s WebMatrix Site
Unrecognized attribute ‘targetFramework’ error
Yesterday while publishing a WCF Service to IIS I got this error from the VS2010 auto generated web.config file.
<compilation debug="true" targetFramework="4.0">
After searching I found it is because of the Framework version setting in the Application Pool of the web application.
In IIS 7, you can modify the application pool by selecting the Application Pools node from left side bar, and Select the Basic Settings, which will bring the Edit Application Pool dialog, where you can choose the .Net Framework Version. To fix the issue, it should set to version 4.x
Creating PDF file using C#
Most of the forums, it’s a common question that how can we create PDF files from C#. Few days back I got a chance to work with iTextSharp library which helps to create PDF files from .Net. You can download the iTextSharp from sourceforge.net. It also supports HTML Parsing too.
Here is the code which converts and HTML File into PDF using iTextSharp libraries.
using System.Collections.Generic;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text.pdf;
class Program
{
static void Main(string[] args)
{
//Creating the instance of the document object.
Document doc = new Document();
//Creating the PDF File
StreamWriter streamWriter = new StreamWriter(@"C:\Sample.pdf");
PdfWriter.GetInstance(doc, streamWriter.BaseStream);
doc.Open();
IEnumerable<IElement> elements;
//Reading the HTML Contents
using (StreamReader streamReader = new StreamReader(@"C:\SamplePage.htm"))
{
//Parsing HTML Contents.
elements = HTMLWorker.ParseToList(streamReader, new StyleSheet());
}
foreach (IElement item in elements)
{
doc.Add(item);
}
//Custom Text which will be appended using Paragraph class.
string paragraph = "This is custom text which will be appended";
doc.Add(new Paragraph(paragraph));
//Closing the document.
doc.Close();
}
}
Thanks to Rahul for providing initial inputs on iTextSharp library.
Difference between HTTP Handlers and HTTP Modules
HTTP modules differ from HTTP handlers. An HTTP handler returns a response to a request that is identified by a file name extension or family of file name extensions. In contrast, an HTTP module is invoked for all requests and responses. It subscribes to event notifications in the request pipeline and lets you run code in registered event handlers. The tasks that a module is used for are general to an application and to all requests for resources in the application.
You can get more information on MSDN – HTTP Handlers and HTTP Modules Overview
FileNot Found exception – Application_Error event in Global.asax
Today I got a problem from my colleague, that he is getting FileNot Found exception in Application Error event in Global.asax, which is used to log application errors. But the application was working fine and good. We thought it may be because of missing images that was referring in Stylesheets. But all the image references are valid and we couldn’t find any issue. We couldn’t find anything suspicious in stack trace.
Stack Trace from the exception
at System.Web.StaticFileHandler.GetFileInfo(String virtualPathWithPathInfo, String physicalPath, HttpResponse response)
at System.Web.StaticFileHandler.ProcessRequestInternal(HttpContext context)
at System.Web.DefaultHttpHandler.BeginProcessRequest(HttpContext context, AsyncCallback callback, Object state)
at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
After doing some search we got an option where we can find the file causing the exception. We created a string variable and put a break point over there and debugged the code. It will display the filename causing the exception. In our case it was a HTML file which was used in a Javascript to set an IFRAME source, unfortunately the developer who copied the script missed the HTML and it was causing the exception.
Here is the code.
void Application_Error(object sender, EventArgs e)
{
Exception err = Server.GetLastError();
//Insert a break point and run in debug mode.
string fileName = Context.Request.FilePath;
Application["UnhandledException"] = err;
}
