Tuesday, October 2, 2018

Check machine status via powershell command

<#$ok = Test-Connection '10.19.999.99' -Count 1 -Quiet #> <# some crappy ip #>
$ok = Test-Connection '10.19.193.29' -Count 1 -Quiet <# actual ip #>
if (-not($ok)) {
'Machine is down' } else { 'All good' }

Thursday, April 27, 2017

Debugging IIS hosted ASP.NET application in visual studio

For large projects it might not be imperative to debug the asp.net application in visual studio. Desired piece of code can be debugged by attaching IIS process (w3wp.exe) to the visual studio. Follow below steps.
- Setup the website in IIS (Make sure, you can browse the website locally)
- Open the project in visual studio
- Click Debug->Attach to process
- In the Available Processes select w3wp.exe. (Make sure, 'Show processes from all users' is checked)
- Now place a break point.
- Open the application in browser and navigate to the corresponding page, that should hit the break    point.


Sunday, December 15, 2013

Redirecting HTTP to HTTPS in mvc

Make sure you have ssl certificate installed and both http and https bindings are set for the website.


In the Global.asax;

  protected void Application_BeginRequest()
        {            
            if (!Context.Request.IsSecureConnection)
                Response.Redirect(Context.Request.Url.ToString().Replace("http:", "https:")); 
        }

If you want to make it conditional to the production only, then using the DEBUG switch is an option;

 protected void Application_BeginRequest()
        {         
            #if !DEBUG
            if (!Context.Request.IsSecureConnection)
                Response.Redirect(Context.Request.Url.ToString().Replace("http:", "https:"));
            #endif
        }

Adding or Removing HTTPS Binding

If you are trying to delete a website that is using a wildcard ssl certificate, Or you are trying to remove ssl binding from a website via IIS, you may come across
following type of warning messages.

The certificate associated with this binding is also assigned to another site's binding. Deleting this binding will cause the HTTPS binding of the other site to be unusable. Do you still want to continue?

OR

The certificate associated with this binding is also assigned to another site's binding.  Editing this binding will cause the HTTPS binding of the other site to be unusable. Do you still want to continue?

Solution is to use command prompt to add or remove ssl binding. (Following command was tested on IIS7)

Adding Https Binding

c:\windows\System32\inetsrv>appcmd set site /site.name:"YOURWEBSITE.COM" /+bindings.[protocol='https',bindingInformation='*:443:YOURWEBSITE.COM']

Removing Https Binding

c:\windows\System32\inetsrv>appcmd set site /site.name:"YOURWEBSITE.COM" /-bindings.[protocol='https',bindingInformation='*:443:YOURWEBSITE.COM']


To view all websites/bindings type the following command

appcmd list site

Monday, November 19, 2012

Static Members VS Application Object in Web Application


The reason to provide Application object in ASP.NET was primarily backward compatibility with the Classic ASP so that it is easier to migrate Classic ASP application to ASP.NET.

It is recommended to keep the application wide information in static variables instead of Application Object,  because static variables are faster than you can access an item in the Application dictionary.

More Information : http://support.microsoft.com/default.aspx?scid=kb;en-us;Q312607

Friday, January 28, 2011

Linq to SQL Mapping by decorating classes with special attributes


With LINQ to SQL, you can set up mappings between C# classes and an implied database schema either
by decorating the classes with special attributes or by writing an XML.

Following is a c# 4.0 code that describes first approach i.e. Linq to Sql mapping by decorating classes with special attributes.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq.Mapping;
using System.Data.Linq;

[Table(Name="Jobs")] public class Job
{
[Column(IsPrimaryKey=true, IsDbGenerated=true, AutoSync=AutoSync.OnInsert)]
internal int JobID { get; set; }
[Column] public string JobTitle { get; set; }
[Column] public long Salary { get; set; }
}

[Table(Name="Applications")] public class Application
{
[Column(IsPrimaryKey=true, IsDbGenerated=true, AutoSync=AutoSync.OnInsert)]
internal int ApplicationID { get; set; }
[Column] public int JobID  { get; set; }
[Column] public DateTime DateApplied { get; set; }
}

String ConStr=”"Data Source=localhost;Initial Catalog=JobsDB;Integrated Security=SSPI;"
DataContext dc = new DataContext(connectionString); // Get a live DataContext
dc.GetTable<Job>(); // Tells dc it's responsible for persisting the class Job
dc.GetTable<Application>(); // Tells dc it's responsible for persisting the class Application
dc.CreateDatabase(); // Causes dc to issue CREATE TABLE commands for each class

Above code will create JobsDB along with tables Jobs and Applications.


Tuesday, December 14, 2010

SilverLight - Fill a ComboBox


Just doing some test

Binding a ComboBox at runtime.
private void button1_Click(object sender, RoutedEventArgs e)
{
List<Car> carList = new List<Car>();
carList.Add(new Car() { Name = "Ferrari", Price = 150000 });
carList.Add(new Car() { Name = "Honda", Price = 12500 });
carList.Add(new Car() { Name = "Toyota", Price = 11500 });
comboBox1.ItemsSource = carList;
comboBox1.DisplayMemberPath = "Name";
comboBox1.SelectedIndex = 2;
comboBox1.SelectionChanged += new SelectionChangedEventHandler(comboBox1_SelectionChanged);
}
void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
Car selCar =(Car) comboBox1.SelectedItem;
MessageBox.Show(selCar.Name);
}
}
public class Car
{
public string Name { get; set; }
public int Price { get; set; }
}

Binding a ComboBox at design time;
<ComboBox Height="23" HorizontalAlignment="Left" Margin="225,184,0,0" Name="comboBox2" VerticalAlignment="Top" Width="120" ItemsSource="{Binding}" >
            <ComboBoxItem Content="Value-1"></ComboBoxItem>
            <ComboBoxItem Content="Value-2"></ComboBoxItem>
            <ComboBoxItem Content="Value-3"></ComboBoxItem>
            <ComboBoxItem Content="Value-4"></ComboBoxItem>
            <ComboBoxItem Content="Value-5"></ComboBoxItem>
</ComboBox>