Simple way to response some json in Webmatrix 2.0? - webmatrix

I'm working on a simple web project for mobile in Microsoft Webmatrix 2.0. For testing purposes, I just want to put a .json file in my project and use that in my javascript code.
Is it possible to set a mime type for different file extensions in Webmatrix? I want my file to response as application/json.

I'll answer my own question. I used to have my code in a blank project, but in order to serve json data with correct mime type, I had to start a new Razor enabled project. I then added a .cshtml file with the following peiece of code:
#{
HttpContext.Current.Response.ContentType = "application/json";
HttpContext.Current.Response.Write("{\"myCollectionOfObjects\":[{...,...}]}");
}

Related

Make json the default formatter in asp.net web api 2

In my web api I allow the user to specify how they want their data via an extension: ie. host/api/location/getmyhouse.json or getmyhouse.xml.
However I also have actions that aren't get and they return if the action was successful or not. For that it doesn't really make sense to specify the return type so I was trying to have it just default to json. However by default it seems xml is used.
Is there a way to default the formatter to json vs xml without removing xml as a formatter AND without having the client specify it as I want this to be done via simple url in the browser. It's an internal thing so that's what we would like.

Getting file inside PCL project always returning null

I am trying to get file which is added in PCL project usin PCLStorgae as below:
IFile file = await PCLStorage.FileSystem.Current.GetFileFromPathAsync("ProjectName.Data.AuthRequest.json");
Above is always returning null. BuildAction is set to Embedded Resource. It is possible to get file inside PCL, but what am I doing wrong here.
Update
Following code working fine without using PCLStorage:
var assem = typeof(Class).GetTypeInfo().Assembly;
Stream stream = assem.GetManifestResourceStream(string.Format("ProjectName.CustomFolder.FileName"));
using (var reader = new System.IO.StreamReader(stream))
{
text = reader.ReadToEnd();
}
Not sure why its returning null with PCLStorage.
PCLStorage is an abstraction over the file system differences of the various native platforms (iOS, Android, Windows etc).
Maybe you found the name of the library misleading? The "PCL" in "PCLStorage" means, it allows you to access the platform specific file systems from within your shared code (PCL). It's not about accessing files that are embedded into a PCL as a resource.
As you already figured out correctly, GetManifestResourceStream() is the correct way to go for embedded resources.

Downloading files in asp.net mvc

I have a link that allows the user to download a file. Basically I call the controller action that uses base.File function. I have few different types of files - pdf, doc, text....Is there a contentType I can specify that would work for all these? Or do I need to specify the correct (matching) contentType for each and every file type?
Use "application/octet-stream" for general purposes.

How can I get started using a WSDL file with Visual Studio it's in the root of the project instead of being hosted on the internet somewhere

I've been tasked when integrating a web form into Oracle CRM on Demand (Siebel) using web services. I've been given the WSDL, and some high level documentation from Oracle.
Well, I knew I was in trouble when I tried to add the WSDL as Web Reference and I was asked to enter an URL. I have the WSDL file in the root of the project, but I have no idea how to link to it.
So, I guess that means I need to learn up on web services using C# and Visual Studio. I have a Safari Books Online account, so I can look up most anything.
It's been a while, but I'm OK at the form part. I just need help connecting and using the web service.
Edit #1: Ok, to clarify my question: I am well aware on how to use web services in general. My specific question is how take this WSDL file and do something with it. If the WSDL was hosted somewhere else, I could just add it as a Web Reference. But, I have the file in the project itself and that is what I am having problems with.
The web reference asks for a URL but you can point it to a local file. Just paste the local file path of your WSDL in and it should work.
Further Clarification of Web Reference URL vs URL to access Web Service
Web Reference URL is used to generate and update wrapper classes for WSDL/Web Service. It is not the URL used at runtime to access Web Service.
URL property on generated wrapper classes is used to access actual web service at runtime.
Update:
It should add a path in the web.config or app.config/settings file (Depending on your project type) similiar to the following:
<setting name="Namespace_WebReferenceName" serializeAs="String">
<value>XXX</value>
</setting>
Which should map to a URL property in the generated web reference wrapper classes. You can modify the URL property programmatically to point wherever you want:
Dim shipService As ShipService = New ShipService() 'Initialize the service - where ShipService is the Generated WebReference Wrapper CLass
shipService.Url = ConfigurationSettings.AppSettings("FedExOnlineURL")

Downloading CSV via AJAX

Can you use AJAX to download a generated csv file from a web application? If so does anyone have any kind of reference that I could be pointed towards?
EDIT: Sorry I should have mentioned I am using Prototype's Ajax.Request and I looked in firebug's response tool and the generated CSV is the response, I just need to get it to pop up with the save file option after has been generated by the Ajax.Request
This is a known limitation of Ajax requests, you will need to use JS like:
window.location='download-csv.rb';
Instead of using an Ajax request. Another way is to change the location of a hidden Iframe, but this has it's own pro's/con's.
You will never get an Ajax request to display the 'file save' dialog, no matter what HTTP headers you send.
In light of your latest edit, to make your CSV file trigger a file download (instead of rendering in the browser), there's no need for Ajax.
Instead, the solution is to have your back-end system add this HTTP header when the CSV file is requested:
Content-disposition: attachment; filename=<your_filename.csv>;
Your implementation here depends on the back-end system you're using. If you're using Rails (as your username suggests), here's a start:
filename = 'your_filename.csv'
headers['Content-Type'] = 'text/plain'
headers['Content-Disposition'] = "attachment; filename=\"#{filename}\""
render :layout => false
Downloading it isn't the problem; you can download any data you like via XmlHttpRequest. The hard part is parsing it. There are several ways to parse it, from regexs to string indexing.
You can use "AJAX" to download anything .. Some people would say you shouldn't call it AJAX in that case since that term is rigorously devoted to downloading XML. But really it's just a mechanism to get data into the client w/o reloading a page. If you were loading HTML it'd be called AHAH, for CSV i guess you'd call it AHAC or AJAC? ..

Resources