Normally, when we stick a JSON file up on an IIS web server, all we have to do to get to is is to set the Mime type.  One easy way to do it is to add to your web.config the following:

 

<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<modules runAllManagedModulesForAllRequests="true" />
<staticContent>
<mimeMap fileExtension=".json" mimeType="application/json" />
</staticContent>
...

 

This works great as long as the GET verb is used (or just enter the on the url like http://mysite.com/myfile.json).

So, what if you "need” to use the POST keyword.  Say for example, you can not change the JavaScript file to use GET instead of POST to get the file.  I was hoping to find some simple web.config parameter to set but I had no luck.  I even posted to StackOverflow and so far, the answers have been less than helpful.  Who knows, maybe by the time you read this, there will be a better answer then my solution which is to write a simple asp.net handler and register it to type json.

 

So, here is the simpler handler that does the trick:

 

public class JSONHandler : IHttpHandler
{

public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "application/json";
string output = System.IO.File.ReadAllText(context.Request.PhysicalPath);
context.Response.Write(output);

}

public bool IsReusable
{
get
{
return false;
}
}
}

 

and the associated web.config entries

 

<httpHandlers>
<add verb="*" path="*.json" validate="false" type="JSONHandler" />
</httpHandlers>
</system.web>

<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<modules runAllManagedModulesForAllRequests="true" />
<staticContent>
<mimeMap fileExtension=".json" mimeType="application/json" />
</staticContent>
<handlers>
<add verb="*" path="*.json" name="JSONHandler" type="JSONHandler"/>
</handlers>
</system.webServer>

I’m assuming there is a simple way, but for now, this works for me.  Please post a better solution and reference it or just tell me in the comments.

Let’s say you have a user input field that you want the user to type data into which will be later displayed back to the user.  You don’t want the user putting in their own html or other things (like javascript tags) because that could cause bad things to happen on your page. 

The easiest thing to do is to set the page attribute to not do request validation

 
<%@ Page Title="" Language="C#" MasterPageFile="~/DefaultNoColumns.master" AutoEventWireup="true" ValidateRequest="false"
CodeFile="SponsorInformationEdit.aspx.cs" Inherits="SponsorInformationEdit" %>


Then, store whatever the user types in the textbox including the nasty things like <script …

When you get around to displaying the data back, simply encode it like this:

LabelShortDescription.Text = HttpUtility.HtmlEncode(rec.CompanyDescriptionShort);

Then, if the user put a bold tag in the html, they will get this displayed back:

 

image

and no harm will occur.

 

Scott Hanselman posted a while back about a browsers definition problem with some ASP.NET sites that causes problems.  Microsoft has articles on this also.  Having just received my BUILD tablet that includes IE10, I tested the Silicon Valley Code Camp web site and discovered that when you login, then switch to another page, the login did not stick.  After uploading the two files Scott mentions (firefox.browser and ie.browser), the problem goes away.

Thanks Scott for the heads up.

 

image

The What and Where

Something that may not be obvious is if are creating an asp.net WebForms project and you put a datasource such as SqlDataSource or ObjectDataSource for example on the page, how can you prevent the SqlSelect associated with that datasource from being triggered.

The answer is to set the control’s visible property to false.  That’s it!

(more…)

I’m trying to download a base64 encoded image that is about 4 Megabytes embedded inside a JSON object.  When I first tried this from my MVC3 Web application, I got the error:

Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property.

 

(more…)

So, here are the steps necessary to install NuGet on my windows laptop computer.  I’ve got vs2010 (not sp1) installed, I’ve disabled both Resharper and .net Reflector just to be safe.

First step, is to go to the NuGet.org web site and click on Install.

image

 

Accept the default:

 

image

 

Run Visual Studio 2010 again, then go into Tool/Extension Manager and select “Automatically check for updates to installed extensions” as follows:

 

image

 

Then, go to View / Other Windows / Package Manager and you will get the Nu-Get prompt.

image

 

Now, you’ have the NuGet prompt as follows.

 

image

At the PM prompt, to install scaffolding, start typing the following:

Install-Package mvc  {then press tab for auto expansion}

You’ll get a list of all packages that begin with mvc as follows:

image

I choose MvcScaffolding, press enter and.. you’ll get the error that you need to have a project open.  Of course, because it wants to add it to your project.

 

image

 

Now, let me add a real project with a simple class called MyTeam

public class MyTeam
{

[Key]
public int ID { get; set; }

[Required]
public string Name { get; set; }
public string City { get; set; }
public DateTime Founded { get; set; }
}

Now, when I re-run the last command, I get all my Controllers and Views.

image

Hope this helps, I have a lot more to learn now.

Introduction

If you’ve started using SqlAzure for your SqlServer with your Azure application, you’ve probably discovered that you get a reasonable number of connection failures.  The advice from the Azure team is add retry logic to all your connections to SqlAzure. There is a long discussion posted by the Azure team here.

The key paragraph states the problem as follows:

The Problem
One of the things that SQL Azure does to deliver high availability is it sometimes closes connections. SQL Azure does some pretty cool stuff under the covers to minimize the impact, but this is a key difference in SQL Azure development vs. SQL Server development.

Basically, what this means is that you must be able to deal with connections failing when you call SqlAzure.  Something that all of probably should have been doing forever, but because most of the time SqlServer is running on your local LAN and the likelihood if a connection failing was next to zero unless something else was going terribly wrong.  Certainly not something we had to do on regular basis.  To emphasize that even more, most of the controls built into asp.net that open connections to sqlserver don’t even do this and that’s from Microsoft itself.

The solution proposed in the thread mentioned above basically has you add tons of code to everyplace you access a connection object.  Personally, I don’t like that because I have hundreds if not thousands of places I open connections and inserting tens of thousands of lines of extra new untested code is a little scary.

So, what to do?

Fortunately, another team at Microsoft, known as the Windows Server AppFabric Customer Advisory Team published a general purpose solution using Extension Methods and some darn clever coding wrote a great article and published code including azure examples that solves this problem very elegantly without requiring a lot of changes to your existing code base.

In this article I plan on giving an example and publishing a sample project that uses this code with SqlAzure to solve the connection retry problem.  My goal here is not to simply restate what they published but to simply have a very simple concrete example of using their library.

(more…)

MSDN has a very nice article on how to create a windows service that hosts a Windows Communication Foundation (WCF) service.  It explains all the details of doing this in a step by step fashion.  One thing that I often find missing from these articles is the actual Visual Studio project that I can download and play with.  What I usually do is put that together myself (which I’m sure is the author’s intent).

To save anyone some time who wants to do the same thing, I’ve created a VS2010 project from the example, added a very simple Windows C# console application that consumes the service, as well as made some small changes in a very nice Windows Presentation Foundation (WPF) calculator project so that the calculator does it operations inside the windows service rather than in the calculator itself.

In this article, I’ve attached the source code (with my small changes and additions) for you to work with and change as you like.

(more…)

Over the past couple years, the focus of the web development I’ve been doing involves building highly flexible, highly scalable and straight forward web sites to implement and maintain Line of Business (LOB) applications.  As you can probably tell from my posts, I’m very “practical” focused, and at the same time have a desire to build awesome web applications.

The technology pairing I’ve chosen is Microsoft’s .Net platform with MVC on the server, and ExtJS on the client.  Though it’s possible to still use ExtJS with standard html/aspx pages, I’ve found the best combination is to use 100% JavaScript on the client (ExtJS) and have all the server side technology be 100% service based.  I’ve used LINQ2SQL extensively as well as Entity Framework in the latest Visual Studio 2010 release.

The learning curve was quite steep to actually be able to efficiently build highly flexible, highly scalable applications using these technologies, but now that I know it, I wouldn’t have it any other way.

I’m considering putting together a series of 4 Day Classes around the country (or even world) that would basically teach people the methods and patterns I’ve learned and essentially leap frog a development team into being able to quickly do what it has taken me years to figure out.  I’ve been fortunate enough to know the top 1% instructors and I’m sure with the right incentive, can get them to join me in both putting together these classes as well as teaching them.

(more…)

© 2012 PeterKellner.net. All Rights Reserved