I have an ASP.Net project using the Razor view engine which currently queries various databases directly with WebMatrix.Data.Database.Open.
I'm trying to migrate all direct database access to Web Services using WCF so that I get consistent business logic. In doing this, I have also enabled the WCF service to be "httpGetEnabled". This allows me to use Excel Macros to query the same web service.
My question is, what is the best way to query these web services from a Razor/WebMatrix page?
Should I JSON enable my WCF service and use the WebMatrix JSON helper? Or should I be using JQuery to get the xml?
I would assume the former would be the best way, but how do I JSON enable my WCF service? I remember seeing a video that allowed me to specify an additional query parameter to the WCF request to get JSON, but I can't find it again.
Any thoughts or pointers in the right direction would be greatly appreciated.
Kind regards.
Edit:
I've recently seen this on Stack Overflow : Client Sid开发者_JAVA技巧e Binding using by Converting the WCF Services to JSON
On following this, I went to the MSDN article referred to around WebHttpBehavior. I have since added "automaticFormatSelectionEnabled=true" to the webHttp element in Web.config. In addition, I added the following code which then allowed me to specify "format=json" as a query parameter:
string formatQueryStringValue = WebOperationContext.Current.IncomingRequest.UriTemplateMatch.QueryParameters["format"];
if (!string.IsNullOrEmpty(formatQueryStringValue))
{
if (formatQueryStringValue.Equals("xml", System.StringComparison.OrdinalIgnoreCase))
{
WebOperationContext.Current.OutgoingResponse.Format = WebMessageFormat.Xml;
}
else if (formatQueryStringValue.Equals("json", System.StringComparison.OrdinalIgnoreCase))
{
WebOperationContext.Current.OutgoingResponse.Format = WebMessageFormat.Json;
}
}
I now just need to work out how to use the WebMatrix JSON helper to get the results... Will update when I get some more information.
Edit 2
Hmm, I don't think that helped... Now I can get JSON from the webservice, but the JSON web helper only seems to help if you have some JSON data already. I'm just not sure how to get that JSON data which I can then use the JSON Helper to convert it (decode) into a class.
So I guess my question now is, how do I get JSON data and then bridge that to WebMatrix/Razor for use in a WebGrid?
From all I read you would want to know what is the best practice in this case if I am correct.
The best practice from what I know is to not make your website query through the wcf at all.
why ? -Because their both located on your server... no point creating a connection again to just get data. They should both have direct connection to the data which for you is webmatrix.... You would use WCF for other applications like a .exe you created which stores farm animals.
Extra note: you can add the WCF to your ASP.NEt project and both access same database which would be best case if you still require it.
精彩评论