AutoMapper ignore attributes from mapping

This is an informative post because I was not able to find this information with above title and most of the time we try to search with this title phrase only.

When you are using AutoMapper and want to ignore some fields from the mapping then there is a simple solution provide from Automapper version 5.0 and later. Keep on reading

Disable automatic table creation in Entity Framework

Usually I prefer code first approach which in turn leads to create or update database based on the any change that I do in my entity model classes. This is very straight forward.

But in one of my recent project for and enterprise client they asked not to do any change in database from code as database should be maintained by the DBA team only so I have to disable database creation and upgrade from my code. To do that I simply have to update one line in my code that is Database.SetInitializer. Keep on reading

AutoMapper configuration in older and newer versions

Recently when I was looking for various architecture approached and tools sets, I saw this post which is very useful. But while I was going through the post I was getting error while implementing AutoMapper. I started getting below errors when I tried to configure AutoMapper as given on the link.

  • The type or namespace name ‘Profile’ could not be found (are you missing a using directive or an assembly reference?)
  • ‘ViewModelToDomainMappingProfile.ProfileName’: no suitable method found to override
  • ‘ViewModelToDomainMappingProfile.Configure()’: no suitable method found to override
  • ‘DomainToViewModelMappingProfile.Configure()’: no suitable method found to override
  • ‘Mapper’ does not contain a definition for ‘CreateMap’
  • ‘Mapper’ does not contain a definition for ‘CreateMap’

After some time I figured out the problem. Actually while following the tutorials I installed latest version (7.0.1 at that time) of AutoMapper from nuget while this tutorial was using 3.3.1 (that article was written sometime in 2015). Keep on reading

URL Rewriting in ASP.NET web form using URL Rewrite module of IIS

Whenever we talk out SEO friendliness of any website first thing we should think about is the URLs. URLs play big role in any website’s SEO score. If you have SEO friendly URLs half of your work is done (don’t take it literally, I am just giving example).

So for creating SEO friendly URLs first thing we have to do is install URL Rewrite extension on our IIS. You can get one from here. You will find different option for downloading and installing extension. You can choose if you want to install using web platform installer or standalone installer. You will also get samples at the bottom of the page linked above.

IIS URL Rewrite module works quit similar to Apache Mod Rewrite and you will see if you are familiar with Mod Rewrite.

After installing extension just create an application on IIS. Open IIS Manager right click on Default Website and click add application. Here you have to give an alias name and select physical location of you application on hard drive. Click Ok.

=&0=& After adding application if connection panel is not showing your application then right click on Default website and select refresh.

Now you have to add a web.config file on root of you application if not there already. Now you can add your rules in web config file.

We will start with a basic rule. Which will work if you want to make you ASP.NET web form application URLs to look like MVC URL. For this we will add this rule:

<configuration>
	<system.webServer>
		<rewrite>
			<rules>
				<rule name="MVC Basic" stopProcessing="true">
					<match url="/(.*)/(.*)/(.*)" />
					<conditions logicalGrouping="MatchAll">
						<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
						<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
					</conditions>
					<action type="Rewrite" url="{R:1}.aspx?id={R:2}&amp;name={R:3}" />
				</rule>
			</rules>
		</rewrite>
	</system.webServer>
</configuration>

After adding this code in web.config if you will open below URL:

http://localhost/YourApplicationName/student/111/John

This will be converted to:

http://localhost/YourApplicationName/student.aspx?id=111&name=John

So your request will ultimately go to your page name as first parameter. In above example it is student to request will go to student.aspx and rest of the parameters will be passed as query string parameter id and name.

You may notice that we have used a tag named conditions in rule tag. This is generally used to check any precondition before applying our defined rule and action. In above example we have added two condition which works like below:

  • First condition checks if request URL is a file on specified path and we have added negate to true so if there is no file with given path then only our rule will be applied.
  • First condition checks if request URL is a folder on specified path and we have added negate to true so if there is no folder with given path then only our rule will be applied.

And the attribute named logicalGrouping in conditions tag given option to specify that if all the condition defined are true then perform this action. We can also specify if any one of the given condition is true then it will perform an operation.

In our example, we have set logicalGrouping to MatchAll for allowing action only when all conditions are true. If we want to do any action when any one condition is true just set it to MatchAny.

So this was a basic use and implementation of URL Rewriting module in ASP.NET web form application.

Bootstrap Multiselect Dropdown in ASP.NET Web Forms

Today I was working on one of my project and I noticed the need of selecting multiple values in a DropDownList. Being a .NET guy my first thought was to us ListBox because is gives same feature, we can selected multiple values by holding ctrl (control) key. But the reason for not using it was that it is not user friendly and its view is not good.

After searching for few minutes I found this Bootstrap Multiselect plugin. I was using bootstrap in my project as well so that was best fit for me. For other also it is very good tool and very easy to use with any platform. I used it with ASP.NET web forms so showing the same here.

You can download the Bootstrap Multiselect plugin from here. Ofcourse JQuery library need to be included first. Along with that you also have to include Twitter Bootstrap javascript and CSS library. After adding JQuery and Twitter library you can add Multiselect plugin file.

This will be something like this

<script type="text/javascript" src="js/jquery.min.js"></script>
<link rel="stylesheet" href="css/bootstrap.min.css" type="text/css"/>
<script type="text/javascript" src="js/bootstrap.min.js"></script>

<link rel=”stylesheet” href=”css/bootstrap-multiselect.css” type=”text/css”/>
<script type=”text/javascript” src=”js/bootstrap-multiselect.js”></script>

Now you have to add a ListBox on your aspx page and keep in mind that SelectionMode should be set to Multiple. Otherwise it will not allow you to select multiple values.

<asp:ListBox ID="ListBox_MultiSelect" runat="server" SelectionMode="Multiple">
     <asp:ListItem Text="Option 1" Value="1"></asp:ListItem>
     <asp:ListItem Text="Option 2" Value="2"></asp:ListItem>
     <asp:ListItem Text="Option 3" Value="3"></asp:ListItem>
     <asp:ListItem Text="Option 4" Value="4"></asp:ListItem>
     <asp:ListItem Text="Option 5" Value="5"></asp:ListItem>
</asp:ListBox>

After adding library references and the ListBox control on your web form you have to call the plugin method to apply it on your control. Remember this call of plugin should come any place below you control on page.

<script type="text/javascript">
    $(document).ready(function () {
        $('#<%=ListBox_MultiSelect.ClientID%>').multiselect({
            includeSelectAllOption: true,
            allSelectedText: 'No option left ...',
            enableFiltering: true,
            filterPlaceholder: 'Search for something...'
        });
    });
</script>

Now you are ready use this plugin so just press F5 and see the magic. You will see somthing like this:

While loading the plugin you can provide various parameters based on your needs. For example in above sample you can see four parameters.

  1. includeSelectAllOption – Adds one option on top of the options list which will select all options upon click
  2. allSelectedText – This will allow to set the text of the Select All option entry
  3. enableFiltering – If this is set to true you will see a text field which will allow you to search through the list options. This is useful for dropdowns with large number of options.
  4. filterPlaceholder – This gives option to set filter text field place holder.

These are just 4 examples of parameters but there are many more option to customize this plugin and you can use them as per your needs.

Solution : Cross domain policy issue while calling SOAP service from your application

Some days back I was trying to call a .net SOAP web service from my Silverlight application. This was my first attempt to call a SOAP service from a Silverlight application and I saw this error

An error occurred while trying to make a request to URI ‘https://edunyte.com/MyService.asmx’. This could be due to attempting to access a service in a cross-domain way without a proper cross-domain policy in place, or a policy that is unsuitable for SOAP services. You may need to contact the owner of the service to publish a cross-domain policy file and to ensure it allows SOAP-related HTTP headers to be sent. This error may also be caused by using internal types in the web service proxy without using the InternalsVisibleToAttribute attribute. Please see the inner exception for more details.

After some hours of searching I found a solution. It was just like I didn’t tell my server that how to manage the requests from other domain. This problem only occurs when you will try to call a SOAP web service from any client side application like Silverlight, flash etc.

So the solution here was just a little XML file that we need to place on our domain root folder. Like as on my domain https://edunyte.com I have to put this XML file named crossdomain.xml on root of the domain and content of the file should be like this:

<cross -domain-policy="-domain-policy">
                <allow -http-request-headers-from="-http-request-headers-from" domain="*" headers="*">
                </allow>
</cross>

This will allow any application from any domain to call your web service. If you want to restrict your service for some specific domain you can change the domain name from “*” to some specific domain which you want to allow for this web service in crossdomain.xml.