Tag: howto

Securing ELMAH with ASP.NET MVC

So I like Elmah and its logging of my unhandled exceptions suer, but I don’t need every yahoo with visibility to that. This is the steps I took to only allow Elmah access to users in the “Admin” role and have it still work with ASP.NET MVC.

Put this into the web.config;

  
    
      
        
      
      
        
        
      
    
  

Now put this into your global.asax, up top (before all the other routing) is best;

            routes.IgnoreRoute("admin/elmah.axd");
            routes.IgnoreRoute("admin/{resource}.axd/{*pathInfo}");

You don’t have have any controller action setup, the handler builds this on the fly to handle the incomding axd request. Nice and simple.

Enjoy!

VN:F [1.9.3_1094]
Rating: 0.0/10 (0 votes cast)

Simple merging object to object

I use View Models to hand to my MVC views, nice and slim with little if any methods on them and I use a think ORM model that has all my business logic and is generated by my ORM (BLToolkit).

So the problem is I wanted to take the View Model and update the ORM model with the modified values from the post, kind of a reverse AutoMapper (or BLToolkit.Mapper).

So a ORM class like this;

    public class Contact
    {
         public int Id { get; set; }
         public string Firstname { get; set; }
         public string Lastname { get; set; }
         // Loads of other properties
    }

And a View Model like this (note the type and name should be “EXACTLY” the same;

    public class ContactModel
    {
         public int Id { get; set; }
         public string Firstname { get; set; }
         public string Lastname { get; set; }
    }

Then with this generic Extension method;

        public static void MergeWith(this T1 primary, T2 secondary)
        {
            foreach (var pi in typeof(T2).GetProperties())
            {
                var tpi = typeof(T1).GetProperties().Where(x => x.Name == pi.Name).FirstOrDefault();
                if (tpi == null)
                {
                    continue;
                }
                var priValue = tpi.GetGetMethod().Invoke(primary, null);
                var secValue = pi.GetGetMethod().Invoke(secondary, null);
                if (priValue == null || !priValue.Equals(secValue))
                {
                    tpi.GetSetMethod().Invoke(primary, new object[] { secValue });
                }
            }
        }

So you make the magic in the Controller (for example) like this;

[Authorize]
[HttpPost]
[ValidateAntiForgeryToken]
[ValidateInput(false)]
public ActionResult EditContact(ContactModel model)
    Contact c = Repo.Get<Contact,long>(model.Id);
    c.MergeWith<Contact,ContactModel>(model);
    Repo.Update<Contact>(c);
}

Yea its cheesy, but it works for me ;)

Enjoy!

VN:F [1.9.3_1094]
Rating: 0.0/10 (0 votes cast)

Nice javascript to set window size for popups

This is some spiffy javascript to get the proper sizes of the window for say a nifty colorbox popup;

    var myWidth = 0, myHeight = 0;
    function setWindowSize() {
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}
};

An example of using it is like this;

$.fn.colorbox({
    iframe: true,
    open: true,
    href: '/Controller/Action',
    width: myWidth - 40, // just a bit smaller than the window
    height: myHeight - 40 // just a bit smaller than the window
});

Dont forget to bind the window resize to reset your size variables;

$(window).resize(function(e){
    setWindowSize();
});

Enjoy!

VN:F [1.9.3_1094]
Rating: 7.0/10 (1 vote cast)

Two forms same page?

I have a page that has two forms in the same page, the catch is the second form is nested inside the first form. Well this violates W3C, and it seems that IE8 doesn’t work with it. Color me surprised that IE8 actually is doing what it suppose to do according to the HTML specs.

Anyhow I needed this to work for my form to function and I was limited on time. So here is what I did to make the submit work with IE8.

1. Remove the “Submit” on the Button and just make it plain button;

2. Add some jQuery magic to submit the first (the outer) form;

$("#Save").click(function() {
    var form = $("form:first").serialize();
        $.ajax({
	    type: "POST",
	    url: "/Tickets/Edit/",
	    data: form,
	    success: function(msg) {
	    window.location = '/Tickets/Details/';
	    }
	});
});

Enjoy!

VN:F [1.9.3_1094]
Rating: 0.0/10 (0 votes cast)

Using Fiddler with Localhost and ASP.NET DEV IIS Server

Quick and to the point (the 1234 is the port your local IIS dev server is running on);

http://localhost:1234 <- fails

http://ipv4.fiddler:1234 <- works

Enjoy!

 

VN:F [1.9.3_1094]
Rating: 0.0/10 (0 votes cast)

jqGrid get Row Column Value

This took me a while to figure out, so I am putting it here for prosperity. This is how to get the value of a specific column in a jqGrid selected Row;

ondblClickRow: function(id, rowid) {
    var ret = $("#grid").getRowData(id);
    window.location = '/Clients/Details/' + ret.ClientID;
}

Enjoy!

VN:F [1.9.3_1094]
Rating: 10.0/10 (1 vote cast)

Copyright © 1996-2010 Bits, Bytes And Burps. All rights reserved.
iDream theme by Templates Next | Powered by WordPress