Archive for the ‘ASP.net’ category

Add GZip compression to your asp.net site

November 5th, 2012

I have been recently looking at ways to speed up performance on my website and I came across GZip compression, which is a great way to compress your javascript and css files before they come down to your system. A few of the benefits of this method are:

You can control what files are compressed.
Bots / SE spiders will crawl your pages faster than before
Decrease Bandwidth

Of course there is the drawback of the performance hit your server will take by running the compression on your files as they are being served, so you have to weight that into consideration as well. I have put below the code you have to add to your web.config file to get it going with asp.net, it goes in the section, enjoy:

<httpCompression directory="%SystemDrive%inetpubtempIIS Temporary Compressed Files">
      <scheme name="gzip" dll="%Windir%system32inetsrvgzip.dll"/>
      <dynamicTypes>
        <add mimeType="text/*" enabled="true"/>
        <add mimeType="message/*" enabled="true"/>
        <add mimeType="application/javascript" enabled="true"/>
        <add mimeType="*/*" enabled="false"/>
      </dynamicTypes>
      <staticTypes>
        <add mimeType="text/*" enabled="true"/>
        <add mimeType="message/*" enabled="true"/>
        <add mimeType="application/javascript" enabled="true"/>
        <add mimeType="*/*" enabled="false"/>
      </staticTypes>
    </httpCompression>
    <urlCompression doStaticCompression="true" doDynamicCompression="true" />

Adobe Acrobat (PDF) files not displaying correctly on iPad?

October 30th, 2012

I have been working on an adobe acrobat export routine on the web that uses ComponentOne’s Active Reports tool to generate the files and I ran into a bit of a snag when trying to display the files on the iPad. I have posted the code below:

HttpResponseMessage message = null;
var pdfExport = new PdfExport();
var m_stream = new System.IO.MemoryStream();

var rpt = new YourActiveReport();
rpt.Run();

pdfExport.Export(rpt.Document, m_stream);
m_stream.Position = 0;
message = Request.CreateResponse(HttpStatusCode.OK);
message.Content = new StreamContent(m_stream);

message.Content.Headers.Add("Content-Disposition", "attachment; filename=yourreportname.pdf");
//this is the magic line
message.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
return message;

I put a comment on the line that looks like this:
message.Content.Headers.ContentType = new MediaTypeHeaderValue(“application/pdf”);
I didn’t have that line in before and it will work just fine on standard machines like your desktop in your browser but since the ipad either doesn’t allow plugins inside safari or doesn’t know how to handle attachments the pdf was coming back as garbage, basically displaying the raw postscript in the browser, that wasn’t cool. All you have to do is add that line in and all is well, I hope this helps someone.

Dynamically assign class name to HTML in asp.net razor syntax

August 1st, 2012

IF you are struggling with getting your html formatted correctly in asp.net like I am here is a little tip I found to set your html based on css classes you have in your css file.

@Html.CheckBoxFor(x => data.Selected, new { @class = "checkbox" })

I tried just wrapping the asp syntax in a div and that didn’t give me the results that I was looking for and besides, this syntax is a lot cleaner. There are other HTML options in the razor syntax but I couldn’t locate a good list anywhere, if you know of one, don’t hesitate to leave me a message and I will get this post updated with that information, thanks.

Asp.net site creating new IP address with Windows Azure

July 25th, 2012

If you have a windows azure project that launches a asp.net site and you are running on IIS Express and for some reason it is incrementing  the IP address up another number, I have the solution for you.  According to Microsoft here is how they assign IP addresses:

“Web role deployments are allocated different increasing IP addresses, starting with get 127.0.0.1, while trying to maintain the port specified in the service definition file. If the service definition file defines two web role deployments and specifies that they use port 8081, the compute emulator would attempt to assign 127.0.0.1:8081 and 127.0.0.2:8081 as the endpoints.

If the port specified in the service definition is in use, then compute emulator tries to allocate an alternate port by monotonically increasing the given port number until one is available. In case of port ranges, the compute emulator tries to allocate a range within which there’s at least one port available.

For example, if port 80 is specified in service definition and is in use by another process, but port 81 is free, the web role deployment is allocated 127.0.0.1:81. The compute emulator will issue a warning that the port in use and unavailable to be allocated as defined in the service definition. The warning appears on the console through CSRUN.exe tool, and in the “Output” window” (“View”->”Output”) for Visual Studio users.”

Microsoft seems to be saying that the emulator handles this for you, mine seemed to not, so all I had to do was stop and start the windows azure emulators and all was good.

Need an easy and clean way to export your asp.net data to Excel?

July 18th, 2012

If you are doing any research on how to get data contained in asp.net pages out to excel, the first path you may take, like i did was to set the Response, something like this:

Response.ContentType = “application/vnd.ms-excel”;
Response.AddHeader(“content-disposition”,”attachment; filename=BizImportReport.xls”);

Well, this was producing for me an excel file with all of the response in it which included the entire webpage, and no matter how many different ways I tried to get the data into some kind of form that would export well, it never worked, until I came across a little library that does exactly what I want in a couple lines of code, so it looks something like this:

var objExport = new Export(“Web”);
if (DS.Tables[0].Rows.Count > 0)
objExport.ExportDetails(DS.Tables[0], Export.ExportFormat.Excel, “yourexcelfile.xls”);

Here is the link to this library, enjoy  Excel Export Library

 

Bitten by the ASP.net bug (again)

July 16th, 2012

Just wanted to put this out there in case you run into this yourself when you are setting up asp.net or IIS on your system to make or create websites.  If you get this error: “Failure Changing IIS ApplicationHost.config: IIS7Register failed with HRESULT 800700b7: ‘Cannot create a file when that file already exists. ” and you just don’t know what do to, go and check the applicationHost.config  file located in \windows\system32\inetsrv\config folder and change these lines:

<add path=”%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll” allowed=”true” groupId=”ASP.NET v4.0.30319 (32-bit)” description=”ASP.NET v4.0.30319 (32-bit)” />
<add path=”%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll” allowed=”true” groupId=”ASP.NET v4.0.30319 (32-bit)” description=”ASP.NET v4.0.30319 (32-bit)” />

Change the %windir% to your local C:\Windows path and save that and re-run your install of asp.net and all should go as expected.  You can re-install it using this command: C:\Windows\Microsoft.NET\Framework\v4.0.30319>aspnet_regiis -i

Hope this helps.