Shadow Copying Assemblies .Net

What is it? Its a .Net feature. When shadow copy is enabled on an appdomain, assemblies loaded in that appdomain will be copied to a shadow copy cache directory, and will be used from there. This is great because the original file is not locked. So it can be changed at will. ASP.NET uses this extensively. Actually, shadow copy is enabled on every appdomain created by ASP.NET.

More information is here:

https://msdn.microsoft.com/en-us/library/ms404279%28v=vs.110%29.aspx

Catch? One should be careful when an application writes any shadow copy assemblies it should do it in its own user account’s temp directory, not Local System’s or admin.

NoSQL

Martin Fowler & Pramod Sadalage
Some common characteristics of NoSQL databases
1. they don’t use the relational data model, and thus don’t use the SQL language
2. they tend to be designed to run on a cluster
3. they tend to be Open Source
4. they don’t have a fixed schema, allowing you to store any data in any record

Interesting read on Martins bliki and http://nosql.mypopescu.com/

Framework vs Library

A library is essentially a set of functions that you can call, these days usually organized into classes. Each call does some work and returns control to the client.

A framework embodies some abstract design, with more behavior built in. In order to use it you need to insert your behavior into various places in the framework either by subclassing or by plugging in your own classes. The framework’s code then calls your code at these points.

source: Martin Fowler 

Generate msbuild file from solution

Here is a quick way to generate a start up msbuild file from your solutions file.

Launch Visual Studio Command prompt and change directory to your project directory and then type the following 2 commands.
set MSBuildEmitSolution=1
msbuild <<your project>>.sln

This file will be generated next to the solution file, it wil be named .sln.metaproj.

All thanks to original post at http://www.codeproject.com/Tips/177770/Creating-MSBuild-projects-from-sln-files

Enjoy

++ operator in VB.Net

We do miss the power of ++ operator in Vb.Net. Here is a quick way to achieve the same result
Use System.Math.Max(System.Threading.Interlocked.Increment(i),i – 1)
to achieve the same result as i++

Hope it helps!

How to run asp.net development server from command line

With help of couple of posts on web and stackoverflow.com

to run asp.net 2.0 webdev server, copy the following command in a .bat file

C:\Windows\Microsoft.NET\Framework\v2.0.50727\Webdev.WebServer.exe /port:[PORT NUMBER] /path: [PATH TO ROOT]

to run asp.net 4.0 webdev server, replace the location of webdev server

“C:\Program Files\Common Files\Microsoft Shared\DevServer\10.0\Webdev.WebServer40.exe” /port:[PORT NUMBER]/path:[PATH TO ROOT]

Hope it helps!

 

 

error : The Web Application Project * is configured to use IIS. The IIS Web server is not installed on this computer.

This problem arises when you try to load an existing web project, configured to run on IIS. A simple quick solution can be to open the project in a text editor and update the property UseIIS from True to False. <UseIIS>False</UseIIS>

Save the file and reload the project. This will enable you to use WebDAV server instead of IIS to run the application.

Happy programming!

Add custom messages to Validation Summary in asp.net mvc form

Sometimes you might need to add messages to Validation summary. Here is a small javascript function that will help you do that. The method can be called onClick event of a button.

    function performValidation() {
        if ($("#notestext").val() == "") {
            var ul = $("#validationSummary ul");
            ul.html("");
            ul.append("<li>" + "Action Note must be entered." + "</li>");
            return false;
        }
        return true;
    }

Hope it helps

How to find all values that have changes in mvc object

‘Compare the original and new versions of any db model, return a string of changes
    Function GetChanges(ByRef database As YourCaretakerEntitiesByRef originalValues As ObjectByRef newValues As ObjectAs String
        Dim returnString As New StringBuilder()
        Dim comparer As ObjectStateEntry = database.ObjectStateManager.GetObjectStateEntry(originalValues)
        For Each changedField In comparer.GetModifiedProperties()
            returnString.Append(changedField)
            returnString.Append(” changed from “)
            returnString.Append(comparer.OriginalValues(changedField).ToString())
            returnString.Append(” to “)
            returnString.Append(comparer.CurrentValues(changedField).ToString())
            returnString.Append(“, “)
        Next
        If returnString.Length > 0 Then returnString = returnString.Replace(“, ““.”, returnString.Length – 2, 2)
        Return returnString.ToString()
    End Function

Handle Access Denied in asp.net MVC

Today I had to add security to the application. Essentially check if the logged in user has got permission to access a function and if not redirect to Unauthorised page. There may be a few ways this can be achieved. Some developers may prefer to add if else check in the view and depending if access needs to be granted they show selective part of the view. This will work, but I think there is a more elegant way to handle this. This is how I achieved this.

Create an attribute that will redirect unauthorised access to a SecurityController. In order to show custom unauthorised messages, it should allow specific messages which can be achieved by passing a reason string.

Here is the code for security attribute: (its in vb.net, but I am sure you can convert to C# easily)

<AttributeUsage(AttributeTargets.Method, AllowMultiple:=True, Inherited:=True)> _

Public NotInheritable Class ApplySecurityAttribute

    Inherits ActionFilterAttribute

    Private ReadOnly _permission As Integer

 

    Public Sub New(ByVal permission As Integer)

        Me.New(permission, String.Empty)

    End Sub

 

    Public Sub New(ByVal permission As Integer, ByVal reason_1 As String)

        _permission = permission

        Reason = reason_1

    End Sub

 

    Public Property Reason() As String

        Get

            Return m_Reason

        End Get

        Set(ByVal value As String)

            m_Reason = Value

        End Set

    End Property

    Private m_Reason As String

 

    Public Overrides Sub OnActionExecuting(ByVal filterContext As ActionExecutingContext)

        If Not PermissionsManager.HasPermission(_permission) Then

            ‘ Put security check here

            ‘ Security Controller

            ‘ Unauthorized Action

            ‘ Put the reason here

            Dim routeValueDictionary = New RouteValueDictionary() From { _

             {“controller”, “Security”}, _

             {“action”, “Unauthorized”}, _

             {“reason”, Reason} _

            }

 

            filterContext.Result = New RedirectToRouteResult(routeValueDictionary)

        End If

 

        MyBase.OnActionExecuting(filterContext)

    End Sub

End Class

Using the attribute is simple. Just declare it on a controller like this:

<ApplySecurity(Enums.Permissions.OfficeUserViewReports, "You are not authorised to view reports")>

Here is the Security Controller class.

Namespace YourCaretaker
    Public Class SecurityController
        Inherits System.Web.Mvc.Controller
        Function Unauthorized(ByVal reason As String) As ViewResult
           ViewBag.Reason = reason
            Return View()
        End Function
    End Class
End Namespace

And finally the permission manager

Public NotInheritable Class PermissionsManager
    Private Sub New()
    End Sub
    Public Shared Function HasPermission(ByVal permissionId As Integer) As Boolean
        'insert your implementation
        'if access allowed Return True else Return False
    End Function
End Class
Posted in .net, asp .net, mvc. Tags: . Leave a Comment »