Being a web developer is not an easy task. Programming in ASP.NET requires a lot of hard work and brains. Many small things seem difficult sometimes and require a lot of time. This could be frustrating and might lead to less productivity. Therefore, this article is aimed at those ASP.NET developers who go through these sorts of situations. It will provide them with some .NET tricks that might be of some help to them.
Our article will provide some .NET tips and tricks that could make your problems disappear. Apart from that, C# tips will also be provided. First of all, I would like to start by sharing some useful .NET tricks with all the readers.
How to send a raw JSON request to ASP.NET from jQuery?
The first step is to build a class in the ASP.NET project and name that SaveData.cs. We execute the SaveData class from IHttpHandler and instigate both, IsReusable and ProcessRequest. After that, you must ascribe the HttpHandler with Web. Config; and for this purpose, you can use ashx to outline the handler.
<httpHandlers>
<add verb="*" path="Save.ashx" type="MyProject.SaveData" />
</httpHandlers>
Once you have ascribed the HttpHandler with the Web. Config, make a jQuery AJAX request and transfer the data to the SaveData handler.
var strJson = JSON.stringify(JsonDetails);
$.ajax({
url: 'Save.ashx',
type: 'post',
async: false,
contentType: 'application/json; charset=utf-8',
data: strJson,
dataType: 'json',
success: function(result) {
alert('success');
},
error: function(result) {
alert('failed');
}
});
In the above example, the JSON item that is supposed to be sent to the Server is kept in the JsonDetails object. To transform the JSON object into a string and transfer it to AJAX, we used JSON.stringify. Now, AJAX has been designed to circulate and transfer the data to the request body. The datatype of the AJAX request has been aligned as JSON.
Now, the browser will call the recently created IHttpHandler with raw JSON data into it. The request is in raw format and parameters cannot be found from the request context. Although, the API is available to deal with this but it is unable to describe the raw JSON object in most cases.
That’s it. Now, when you execute the project and call the AJAX request through JavaScript, the data will be described in the Server and an actual JSON object will be given to you. In order to explain the JSON request by yourself, use the code given below.
public class SaveData : IHttpHandler {
public bool IsReusable {
get {
return true;
}
}
public void ProcessRequest(HttpContext context) {
var jsonString = String.Empty;
context.Request.InputStream.Position = 0;
using(var inputStream = new StreamReader(context.Request.InputStream)) {
jsonString = inputStream.ReadToEnd();
}
JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
object serJsonDetails = javaScriptSerializer.Deserialize(jsonString, typeof(object));
// You can now add logic to work with serJsonDetails object
}
}
Remove persistent cookies before expiration time
Persistent cookies are those forms of cookies that are saved in the user’s hard drive and stay until they expire automatically. This .NET trick would help the developers in removing persistent cookies from the hard drive before its expiration time. The code, given below, will help you in setting the expiration time for cookies.
// Creating a Cookie Object
HttpCookie _userInfoCookies = new HttpCookie("UserInfo");
// Setting values inside it
_userInfoCookies["UserName"] = "Abhijit";
_userInfoCookies["UserColor"] = "Red";
_userInfoCookies["Expire"] = "5 Days";
// Adding Expire Time of cookies
_userInfoCookies.Expires = DateTime.Now.AddDays(5);
// Adding cookies to current web response
Response.Cookies.Add(_userInfoCookies);
The above code would help you in setting the expiration time for the persistent cookies. If you are willing to remove the cookies prior to their expiration time, you just need to override the cookie’s information and for that purpose, the code, given below, is recommended.
HttpCookie _userInfoCookies = new HttpCookie("UserInfo");
// Adding Expire Time of cookies before existing cookies time
_userInfoCookies.Expires = DateTime.Now.AddDays(-1);
// Adding cookies to current web response
Response.Cookies.Add(_userInfoCookies);

Mahesh ChandPosted Nov 25, 2016, 8:36 PM
Welcome to C# Corner, Dasha. The title of this article should not have "Overview Of Microsoft Big Data Solutions" in it. It is incorrect. Please fix it. Thanks.