Posts

Showing posts from July, 2018

Clearing browser cache of script files programmatically

Please add below script into a file called clearcache.js (function () {    var process_scripts = false;     var rep = /.*\?.*/,     links = document.getElementsByTagName('link'),     scripts = document.getElementsByTagName('script');     var value = document.getElementsByName('clear-browser-cache');     for (var i = 0; i < value.length; i++) {         var val = value[i],             outerHTML = val.outerHTML;         var check = /.*value="true".*/;         if (check.test(outerHTML)) {             process_scripts = true;         }     }     for (var i = 0; i < links.length; i++) {         var link = links[i],         href = link.href;         if (rep.test(href)) {             link.href = href + '&' + Date.now();         }         else {             link.href = href + '?' + Date.now();         }     }     if (process_scripts) {         for (var i = 0; i < scripts.length; i++) {             var scr

Customized path change for MVC Views

When we create MVC project all the Models, View and Controllers will be created with separate folder structure. after publishing the MVC you will get folder structure as  BIN Views Images Fonts Styles Scripts Global.asax Packages.config web.config so, we will place all the folders and files into a folder called WebSite1 and map that folder to IIS virtual directory and use it. But if we want to keep another sub folder within the WebSite1 then we have to make changes to the default  return view(). Required folder structure WebSite1 WebUIFileUpload                   BIN Views Images Fonts Styles Scripts Global.asax Packages.config web.config   we can change the default paths using below process public class CustomViewEngine : WebFormViewEngine {     public CustomViewEngine()     {         var viewLocations =  new[] {               "~/Views/{1}/{0}.aspx",               "~/Views/{1}/{0}.ascx",               "~/Views

MVC Project publish Steps

Generally after we create asp.net project we used to deploy the site at IIS by creating virtual directory my mapping physical path code. But MVC project  has different process to publish at IIS we have to follow below steps to create deploy folder MVC Project Publish Steps 1 . Select Project at Solution Explorer and right click on it 2 . Select Publish 3 . Select Profile and Click Next button 4 . Specify the Profile Name as it is temporary to create 5 . Click on Next button 6 . Select the Publish Method from the drop down as "Web Deploy Package" and Click Next button 7 . Specify the Package Location path so that it will place the package in zip 8 . Click on Next button 9 . Specify Site name if you want otherwise leave it as it is not mandatory field and then Click Next button 10. Select the configuration as Release from the drop down 11. Click on Next button 12. See the preview 13. Click On Publish It will publish the entire project and place the deployme

Exception handling in ASP.NET MVC

Exception handling is the process of handle the occurrence of exceptional conditions requiring special processing and displaying the customized messages or logs. Exception handling is important in any application. In ASP.NET we can handle exceptions in the following two ways: Try-catch-finally block at method level Using Application_Error In ASP.NET MVC we have a larger list of ways to handle exception such as: Simple way Override “OnException” method Using “HandleError” Attribute Inheriting from “HandleErrorAttribute” Handling HTTP errors Global Error handling in MVC Simple way The simplest wayis to use the traditional .NET exception handling style i.e. try and catch block. Now when exception happens catch block gets executed and it redirects to the error view. But if we use this method then we will not be utilizing MVC exception mechanism properly and completely. In the further sections we will discuss five important ways by which we can utilize MVC provided feat

New ASP.NET Session_Id creation

When you want it to change session id when someone logs in to website just before you set  initial session variables. try to use below code snippet  SessionState.SessionIDManager Manager = new SessionState.SessionIDManager();  string NewID = Manager.CreateSessionID(Context);  string OldID = Context.Session.SessionID

URL query string length limitations

HTTP states there is no limit to the length of a query string, but it indicates the host name is limited to 255 characters because of DNS limitations. While the specifications do not specify any maximum length, practical limits are imposed by web browser and server software.  Internet Explorer (Browser) Microsoft states that the maximum length of a URL in Internet Explorer is 2,083 characters, with no more than 2,048 characters in the path portion of the URL. Attempts to use URLs longer than this produced a clear error message in Internet Explorer. Edge (Browser) The limit appears to be around 81578 characters.  Chrome It stops displaying the URL after 64k characters, but can serve more than 100k characters. No further testing was done beyond that. Firefox  After 65,536 characters, the location bar no longer displays the URL in Windows Firefox 1.5.x. However, longer URLs will work. No further testing was done after 100,000 characters. Safari At least 80,000 characters

Read file content using javascript

We can upload the file using file control and read content of that file using below script code. <html> <head>     <script>       var openFile = function(event) {         var input = event.target;         var reader = new FileReader();         reader.onload = function(){           var text = reader.result;           var node = document.getElementById('codeInTest');           node.value = text;         };         reader.readAsText(input.files[0]);       };     </script> </head> <body>     <input type='file' accept='text/plain' onchange='openFile(event)'><br>     <textarea rows="17" cols="129" wrap="virtual" id="fileContent"></textarea> </body>

ASP.NET Session_Id Cookie in ASP.NET

“ASP.NET_SessionId” cookie in ASP.NET When a user opens his web browser and makes a request to a ASP.NET Web Application for which a cookie based Session is enabled, Server generates a new Session Id and sends it to web Browser as a cookie with the name "ASP.NET_SessionId". Applications in the same DNS domain share the same Session Id and cookie. When the user browses within the same DNS domain, the browser sends same Session Id and cookie to the domain. Since it is shared across applications in a domain, so ASP.NET doesn't remove the cookie when the session is expired or Session.Abandon() is invoked. Because of this design, the code that is used to check if a Server Session is actually expired doesn't work properly. Generally we use following code snippet to check if session is expired. if (Session.IsNewSession) {     if (Request.Headers( "Cookie" ) != null && Request.Headers( "Cookie" ).IndexOf( "ASP.NET_SessionId"