ashx method call - make reusable - c#

i have an ashx file that creates a binary csv file for me. i'd like to be able to pass it any generic list.
i know i can put the list in session and then pick it up in the generic handler but will that acceptable under heavy loads?
byte[] csvData = Utility.ToCsv(",", DAL.GetProducts());
context.Response.OutputStream.Write(csvData, 0, csvData.Length);
it would be nice if i can run any query within the handler (instead of hard-coding DAL.GetProducts()). what are some ways to do this?

If the dataset is above the threshold of what can be serialised into QueryString data, I would simply create a HttpHandler which accepts the list as serialised POST data. That way all your ToCsv() handler needs to do is get the list out of the post data and then deserialise it.
Let me know if you need me to elaborate on any of this.

Related

C# Dynamic List Class

I have a bit of a weird issue. Working in C# script with SSIS I have developed a need to build a List based off Dynamic Data.
Background
To explain it, a script task is fired that has a variable API URL, this goes off and pulls a JSON string back and then throws it into a strongly typed list using the following code.
var listobject = get_APIData<ApplicationOneDataSet>(url)
The class that does this is long winded and not really needed in the context of this issue.
ApplicationOneDataSet is a strongly typed match to one of the possible JSON results returned by get_APIData.
Now I have a need to change ApplicationOneDataSet to ApplicationTwoDataSet dynamically based on which API URL I pass to the script.
So what I have done is send through a second variable to the script called class name which contains the string "ApplicationDataSetOne" or "ApplicationDataSetTwo" based on which context I call it under.
The Question
My question is how can I dynamically vary this line:
var listobject = get_APIData<ApplicationOneDataSet>(url)
With the string variable passed into the script.
My original thinking was something along the lines of this:
var ClassType = (string) Dts.Variables["AppClassName"].Value;
Type type = Type.GetType(ClassType);
var listobject = get_APIData<type>(url)
Though it doesn't seem to like that. Any tips would be great!
As long as there is exactly two types you can use and you know them at compile time, I would not look further than a simple if. It works, it's easy, everyone understands it.
You can do it totally dynamic at runtime, but that's a huge pain in the... where you don't want it to be. If you really want to go down that rabbit hole, you can find more information here.
I'm not sure I fully understood what you are trying to do, but how about writing an interface ApplicationDataSet and then making a list of it? This way your list is going to be able to contain both types of data.

Use parameters to pass data between pages or extract it again from database?

I am new to ASP.NET and I wonder which would be better? Should I use parameters to pass the value of a field from the database which I have extracted in the current page between pages and get it by Request.Params["abc"] or it would be better if I pass only the ID of the object and then extract it again in the other page from the database?
For exmaple, I need to show the name of a specialty in AnotherPage.aspx. I already have extracted the whole specialty object from the database in SomePage.aspx because I need it there. The question is which of the following is the better approach?
www.mysite.com/AnotherPage.aspx?specialtyId=2
www.mysite.com/AnotherPage.aspx?specialtyName="Methematics"
What are the advantages and disadvantages of the two methods?
Thank you very much in advance!
From a clean URL perspective, it's better to just pass the ID. But even then, you'd want to make it nicer. Instead of...
www.mysite.com/AnotherPage.aspx?specialtyId=2
You'd want
www.mysite.com/Specialties/2
You can do that with URL Rewriting.
Now, that begs the question, what do you do with the data for each Specialty object? You could load that from the database on each request. I'd do that. Then, if performance suffers you might consider caching.

Download XML client-side, fetch data from Session first

My goal is to have the user be able to download some information currently stored in Session. There are multiple key/value pairs in Session I would like to have the user download as one XML file.
The control I am working with has a client-side 'onClick' event exposed. I was planning on calling a PageMethod to extract the data I wanted from Session, and return that data to the client.
I've seen a very clean implementation of this in MVC and I am wondering if this clean implementation is possible in ASP.NET AJAX, as well. If not, I am wondering what the best route would be to go for this.
In MVC/JavaScript I see something like this:
location.href = "../WorkOrders/Export/" + workOrderID;
public ActionResult Export(int id)
{
WorkOrderPdfExporter exporter = new WorkOrderPdfExporter();
byte[] buffer = exporter.Export(id);
return File(buffer, "application/pdf", String.Format("workorder#{0}.pdf", id));
}
This Export method returns a FileContentResult which is an MVC thing. I am wondering if something like this exists in ASP.NET AJAX, and if the datatype is suitable to return for a Page Method.
If not, what should I be doing here? I was thinking about creating a dictionary, sticking the relevant session objects into this dictionary, serializing it to XML (I have a Serializable Dictionary class implemented), ...and then attempting to return that XML for download?
Thanks for your time.
You can write directly to HttpResponse.OutputStream - you will need to set the correct ContentType too.
The code example for OutputStream is rather verbose (mostly dealing with a dynamically created image), but if you ignore those parts, you will have a basic function that will need minor modification for your use.

Passing .CS arraylist to JQuery

I am trying to create an autocomplete text box based on an ArrayList from my main page's cs.
I am fairly new to JQuery and I am wondering what the best way to call the ArrayList from that page would be.
I feel like I have search all terms possible but all I can find are AutoComplete examples that either create the Array in a variable before it executes the script or it makes a vague reference to calling a URL.
Thanks for any information on this.
This is what I have so far in my newby experience.
<script type="text/javascript">
$(document).ready(function(){
$("#example").autocomplete("Requests.aspx.cs");
});
</script>
A few things....
You don't call Request.aspx.cs (that is a code file that gets compiled). The client served page is Request.aspx, and that's what you need to call.
Since you're using the jQuery UI Autocomplete, the Request.aspx will need to respond with JSON data in the correct format (I believe it is a 3 field object with id, name and value).
Request.aspx will need to be setup to take in a parameter (I believe it is called term), do logic to lookup, and return the results of that lookup serialized as JSON. There are C# classes to help with JSON serialization.
You've got quite a few things you need to go figure out in order to achieve this. Hopefully the above will help get you started.
You can create a $.ajax request for the page Request.aspx, whose code behind should return your data. Then use the jQuery autocomplete.
Edit:
If you want to take the approach that Matthew has suggested, I would suggest checking out this page ASP.NET AJAX from ScottGu, as it describes a way to make server-side service calls directly from your javascript and should eliminate any mess JSON handling on your end.
May be you could expose some kind of restful service for this purposes.

Save something in settingsfile or.. ? in C#

I am implementing an application and I have a few lists with some stuff in it, which is always the same, but I don't want to implement this in my real logic stuff.
Is there any way to save these items in your application? I've read some things about saving these items in a settingsfile.
Is this the best way, or there are better ways? and how can I do this?
You can save it in the application settings file
Save in XML. You can bind directly to xml in wpf. See: http://joshsmithonwpf.wordpress.com/2007/06/04/binding-to-xml/
If the content of the list is supposed to never be changed you can make them private static fields in a static class the provides a mthod to create a new instance of the list.
If the content of the list is supposed to be changed, it is a good idea to store it in the application settings file.
If the content of the list is supposed to be changed per user you should save it in the user settings file.
Depending of the content itself, I like to load the content of list into a database table. For example, a list of school would be stored in a table calle T_Ref_Schools. The Ref indicated to me that those values can be changed over time but not often.

Categories