public string ReturnString()
{
string s = "";
s = HttpUtility.SomeValiable;
return s;
}
SomeValiable = <Dependent><DependentId>
But I am getting the string s value like <Dependent><DependentId>
NOTE: I have not applied ; after < as it converted it into <
Might be the string is not compatible with the HTML format. Please suggest me what datatype I have to use to return HTMLDecode format.
ANY QUICK HELP PLEASE
See this question.
The data type you are looking for is XElement or XmlElement as a return type.
Another easy way that can use string as a return type is to use System.Net.WebUtility to htmlencode before returning in the service and htmldecode in the client.
Related
I have an ASP.NET MVC app. My views use Razor. At the top of my CSHTML file, I have the following:
#functions
{
public static HtmlString IsSelectedCss(string name)
{
string selected = ""; // Need to get value of "t" from query string
HtmlString attribute = new HtmlString("");
if (selectedTab.Equals(name, StringComparison.InvariantCultureIgnoreCase))
{
attribute = new HtmlString("class=\"active\"");
}
return attribute;
}
}
I need this function to examine the query string. Specifically, I need to get the value of the "t" query string parameter. My challenge is, I cannot seem to figure out how to get access to the QueryString in this function.
How do I get the value of a query string parameter in a Razor function?
Thanks!
The query string can be gotten from below.
HttpContext.Current.Request.QueryString["t"]
You need to make your function non-static, since the querystring is part of the request.
You can then write
HttpContext.Request.Query["t"]
You should really be doing this in the controller and pushing it through the model. But if you insist, you can simply use:
<%= Request["t"] %>
But why not read it in your controller?!
I have a list of names in a observable collection returned from wcf service and database is oracle, I want to make full text search on the list using LINQ.
service is consumed in silverlight application.
Any suggestions pls.
How about this?
var found = thelist.Where(str => str.Contains(strToSearchFor));
or maybe this -
var found = thelist.Where(str => str.ToLower().Contains(strToSearchFor.ToLower()));
if it is not list of strings it would like like this:
var found = thelist.Where(obj => obj.strProperty.Contains(strToSearchFor));
If you need this solution to be case insensitive the solution of Hogan can be done without creatring a new string (by using the ToLower() method).
First, create an extension method:
public static class Extensions
{
public static bool Contains(this string source, string stringToMatch, StringComparison comparisonType)
{
return source.IndexOf(stringToMatch, comparisonType) >= 0;
}
}
Then you can make the Hogan solution case insensitive like this:
var found = thelist.Where(str => str.Contains(strToSearchFor, StringComparison.OrdinalIgnoreCase));
I'm learning aspnet mvc 4 web api, and find it very easy to implement by simply returning the object in the apicontrollers.
However, when I try to return value types such as bool, int, string - it does not return in JSON format at all. (in Fiddler it showed 'true/false' result in raw and webview but no content in JSON at all.
Anyone can help me on this?
Thanks.
Some sample code for the TestApiController:
public bool IsAuthenticated(string username)
{
return false;
}
Some sample code for the jQuery usage:
function isAuthenticated(string username){
$.getJSON(OEliteAPIs.ApiUrl + "/api/membership/isauthenticated?username="+username,
function (data) {
alert(data);
if (data)
return true;
else
return false;
});
}
NOTE: the jquery above returns nothing because EMPTY content was returned - however if you check it in fiddler you can actually see "false" being returned in the webview.
cheers.
Before your callback function is called, the return data is passed to the jquery parseJSON method, which expects the data to be in the JSON format. jQuery will ignore the response data and return null if the response is not formatted correctly. You have two options, wrap you return boolean in a class or anonymous type so that web api will return a JSON object:
return new { isAuthentication = result }
or don't use getJSON from jQuery since you're not returning a properly formatted JSON response. Maybe just use $.get instead.
Below is a quote for the jQuery documentation:
Important: As of jQuery 1.4, if the JSON file contains a syntax error,
the request will usually fail silently. Avoid frequent hand-editing of
JSON data for this reason. JSON is a data-interchange format with
syntax rules that are stricter than those of JavaScript's object
literal notation. For example, all strings represented in JSON,
whether they are properties or values, must be enclosed in
double-quotes. For details on the JSON format, see http://json.org/.
I've created a WCF Web Service which returns data in JSON format. The code for the service is as follows:
[OperationContract]
[WebGet(ResponseFormat = WebMessageFormat.Json)]
List<MyCustomer> GetCustomerJSON();
And
public List<MyCustomer> GetCustomerJSON()
{
var nm = (from n in _ent.Customers
select new MyCustomer() { CustomerID = n.CustomerID, AccountNumber = n.AccountNumber }).Take(10);
return nm.ToList();
}
However, the output isn't well formed. It included square brackets at start and end.
Because of which I can't use Json Parser tool.
Please help.
If you return List<T> it will be encoded like array of T in the JSON and array will be encoded with respect of square brackets:
[{"strProprety":"bla","intProperty":123,"booleanProperty":true}]
In your case it will be probably
[{"CustomerID":1,"AccountNumber":123},{"CustomerID":2,"AccountNumber":456}]
It is valid JSON. You can use http://www.jsonlint.com/ to verify this. So WCF produce correct output and you have problem only with the "Json Parser tool".
I have to return a lot of values back to my windows application from my webService. But how would I return more than just a single string/int/boolean from my WebService to my Application. How would I return a collection, keyValuePair, or even better, a DataSet? Or is this just imposible?
thank you :)
any serializable class can be returned from a web service
If your webservice is written in ASP.NET it's as simple as setting the return type from your webservice to be the more complex type. Most serializable types are supported.
The best method I've used with webservices is to return XML as a string. This way there are no compatability issues and parsing the XML is easy enough.
Along the same lines as returning XML, if you're returning to javascript code, you may want to consider returning your complex type as serialized in JSON.
Here is an extension method which makes this really easy...
<Extension()> Public Function ToJSON(Of T As Class)(ByVal target As T) As String
Dim serializer = New System.Runtime.Serialization.Json.DataContractJsonSerializer(GetType(T))
Using ms As MemoryStream = New MemoryStream()
serializer.WriteObject(ms, target)
ms.Flush()
Dim bytes As Byte() = ms.GetBuffer()
Dim json As String = Encoding.UTF8.GetString(bytes, 0, bytes.Length).Trim(Chr(0))
Return json
End Using
End Function
Then in your data service, you can simply call
Return MyObject.ToJSON