Posts

Showing posts from January, 2015

ASP.Net MVC: Recursive in the MVC View.

We have used recursive method i the c# code, but have you ever had a requirement where you have to render a view from a recursive data. I got on requirement of that like and i have to render the view data from a tree based data. so for this you need to do like this in your view. @model List<TreeViewModel> @{     View.Title = "TreeData";     Layout = "~/Views/Shared/_Layout.cshtml"; } <h2>Tree Data</h2> @ParseTree(Model, null); @helper ParseTree(IEnumerable<TreeViewModel> nodes, int? parentId) {     if (nodes.Any(n => n.ParentId == parentId)) {     <ul>         @foreach (var node in nodes.Where(n => n.ParentId == parentId)) {             <li>                 @node.DisplayName                 @ParseTree(nodes, node.Id)            ...

ASP.Net MVC: Log out User in case of session timeout with ASP.NET MVC5 with OWIN Authentication.

When you are using OWIN authentication in ASP.Net MVC 5 and also session to store your value. Then you need your user to be redirected to the Login page in case of session expire.  I searched so many blogs and found so many ways to do that, but what fits with my requirement is this what i want to share it with you all. For this there are several steps you need to do as below: Step 1: You need to create a new ActionFilterAttribute. This you need to create inside the Filterconfig class in the App_Start folder and then you need to add you filter in the RegisterGlobalFilters method. You calss to be look like below: public class FilterConfig     {         public static void RegisterGlobalFilters(GlobalFilterCollection filters)         {             filters.Add(new HandleErrorAttribute());             filters.Add(new CheckSessionOutAttribute()); ...

ASP.Net MVC: Encrypting the connection string inside the web.config.

As we all know that the connection string is the vital part of the any application, also so much vulnerable if we have mentioned it as a simple string inside the web.config. So we want that to be encrypted. But to do that there are so many method by which you an do it. ,anually also you can encrypt that by your own logic and decrypt based on that during the creating the conection string or to pass on to the entity framework.  but i found a easiest way to do that, which by default come with .net Framework. aspnet_regiis is the command help us in that. you need to encrypt connection string during the deployment to production. not needed during development, if you don't want to hide the sql info from your developers. Also after encrypting it you can decrypt the same to see the original values. Use aspnet_regiis. In command line browse to (usually): cd C : \WINDOWS\Microsoft . NET\Framework\respective version folder or  you can open the command prompt from Visual studi...

ASP.Net MVC: Dynamically send the Connection string to the Entity Framework Context on DB First approach

Dynamically send the connection string to the Entity Framework context. In some situation we need to pass on the connection string dynamically when needed. But one more thing that the both DB should have same structure. as in case of my project i have more then one connection string inside the web.config. when user try to login he need to select the site name he wants to login and based on that the user is validated. <connectionStrings>     <clear />     <add name="Site1" connectionString="metadata=res://*/HarmonySITContext.csdl|res://*/HarmonySITContext.ssdl|res://*/HarmonySITContext.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=sqlservername;initial catalog=databasename;persist security info=True;user id=sa;password=sa;MultipleActiveResultSets=True;App=EntityFramework&quot;"       providerName="System.Data.EntityClient" />     <add name="Site2" connectionString="meta...