Saturday, September 27, 2008

ASP.NET: How to call webservice with parameters through extJs HttpProxy class.

In this post I am going to explain how we can call a C# asp.net webservice with parameters through extJs HttpProxy class.

You have to do this:

var storeName = new Ext.data.Store

({

proxy: new Ext.data.HttpProxy

({

url: 'webserviceName/methodName,

method: 'POST'

}),

baseParams: {parameter1: p1,parameter2 p2},

reader: readerName,

});

In webservice you have to write:

[WebMethod]

[ScriptMethod(ResponseFormat = ResponseFormat.Json, XmlSerializeString = false)]

public void GetData(int p1, int p2)

{

// outString is a JSON formated string

Context.Response.Write(outString);

}

Labels: , ,

Post a Comment(3 comments)

Nice and simple :D We love to see our community members helping others.

Looking forward to more blog posts!

Aaron Conran
Ext JS Core Developer
That's very nice. Exactly what I was looking for too! Needed that Json part. tyvm
The JSON formated string could be like this:

string outString = "{\"Status\":[";

foreach(DataRow dr in dt.Rows)
{
if(dt.Rows.Count>i+1)
outString = outString + "{" + "\"Status\":\"" + dr["name"] + "\"," + "\"status_id\":\"" + dr["status_id"] + "\"},";
else
outString = outString + "{" + "\"Status\":\"" + dr["name"] + "\"," + "\"status_id\":\"" + dr["status_id"] + "\"}]}";
i++;
}
if (dt.Rows.Count <= 0)
outString = "";

Secure file download in Asp .net

I was trying my hands on Asp .net after a long time, and then came across an interesting problem. I was trying to write a function to make a secure download of web based files, that is I was trying to hide the path of file from user, who is going to download a file from my site. I googled a lot, but could not find the exact thing, that I was looking for. Then I collected the pieces of codes from my search, and created that function out of it. This function can be called from any event and will initiate the download without letting users know the path of target file.

The code goes here:

public void download(string fileName, string parentDirectory)
{
string path = Server.MapPath(pare);
FileInfo f1 = new FileInfo(path + "/" + fileName);
        if (f1.Exists)
        {
            Response.Clear();
            Response.AddHeader("Content-Disposition", "attachment; filename=" + f1.Name);
            Response.AddHeader("Content-Length", f1.Length.ToString());
            Response.ContentType = "application/octet-stream";
            Response.WriteFile(f1.FullName);
            Response.End();
        }
        else
            Response.Write(path + "/" + fileName);
}

Note: To use this code, one must use the System.IO namespace.

Labels: , ,

Post a Comment(0 comments)

Monday, June 30, 2008

.NET Language Integrated Query (LINQ)

The next big challenge in programming technology is to reduce the complexity of accessing and integrating information from relational databases and XML.

Rather than adding relational or XML-specific features to our programming languages at runtime, we can take a more general approach and could add general purpose query facilities to the .NET Framework that apply to all sources of information, not just relational or XML data. This facility is called .NET Language Integrated Query (LINQ).

.NET Language Integrated Query defines a set of general purpose standard query operators that allow traversal, filter, and projection operations to be expressed in a direct yet declarative way in any .NET-based programming language. The standard query operators allow queries to be applied to any IEnumerable-based information source.

The extensibility of the query architecture is that it can be used to provide implementations that work over both XML and SQL data. The query operators over XML (XLinq) use an efficient, easy-to-use in-memory XML facility to provide XPath/XQuery functionality in the host programming language. The query operators over relational data (DLinq) build on the integration of SQL-based schema definitions into the CLR type system.

LINQ is built entirely on general purpose language features, some of which are new to C# 3.0 and Visual Basic 9.0. Each of these features has utility of its own, yet collectively these features provide an extensible way to define queries and queryable API’s.

In general, the developer is free to use named methods, anonymous methods, or lambda expressions with query operators. Lambda expressions have the advantage of providing the most direct and compact syntax for authoring. More importantly, lambda expressions can be compiled as either code or data, which allows lambda expressions to be processed at runtime by optimizers, translators, and evaluators.

Note: Lambda expressions are similar to CLR delegates and must adhere to a method signature defined by a delegate type.

eg for Lamda:

IEnumerable<string> expr = from s in names

where s.Length == 5

orderby s

select s.ToUpper();

The code written above is equivalent to this lamda expression:

Eg IEnumerable<string> expr = names

.Where(s => s.Length ==5)

.OrderBy(s => s)

.Select(s => s.ToUpper());

Labels: ,

Post a Comment(0 comments)