Articles

How to encrypt to a url safe string...

Hello,
Here's a helper that can be used to encrypt/decrypt ids that are url safe.
For example, if your user needs to download an invoice, it's clearly more secure to provide a url like 
http://yousite.com/download-invoice/TVZiSlU0a0VHWnlZR2NTOTRwYy9Sdz09
instead of:
http://yousite.com/download-invoice/13
As there can be a malicious user that will try to download invoices from others, or you don't want the user to know what the invoice id is.
Example, in your controller:


[ActionName("download-invoice")] publicFileResult downloadInvoice(string id)         { var decodeId =int.Parse(id.DecryptBase64()); var invoice = MainService.getInvoiceById(decodeId); if (invoice == null) thrownewHttpException(404, "Not Found"); return File(MainService.generateInvoicePDF(decodeId), "application/pdf");         }      

In your view:
<li>Invoice<atarget="_blank"href="/download-invoice/@Model.invoiceId.ToString().EncryptBase64()"><imgsrc=…

Html to Image...

Sometimes it's useful to generate an image from a url.


Here's how you can do it for free, without having to pay for expensive 3rd party components.


All you have to do is download wkhtmltoimage.exe from

https://wkhtmltopdf.org/downloads.html



       public static byte[] DownloadImage(string url)
        {
            return DownloadImage(url, 1024, 768);
        }

        public static byte[] DownloadImage(string url, int w, int h)
        {           
            var htmlToImageDir = Path.Combine("Your Path", "HtmlToImage");
            var process = new Process();
            var startInfo = new ProcessStartInfo
            {
                UseShellExecute = false,
                RedirectStandardOutput = true,
                RedirectStandardError = true,
                RedirectStandardInput = true,
                CreateNoWindow = false,
                FileName = Path.Combine(htmlToImageDir, "wkhtmltoimage.exe"),
                Arguments = s…

How to delete many-to-many relationship in Entity Framework...

Hi Folks,

I've been searching the internet to find a simple way to delete a many-to-many relationship item using Entity Framework Code-First...

I couldn't find something very useful so I have written  my own code.

The problem is if you try to use a foreach statement you'll be able to delete the first item but not the others, as the collection would be modified.

So I made the following assumption: Two tables, the first is Contenu and the second Tag, with a many-to-many relationship in SQL Server, I called the table ContenuTag, but this table won't appear in our model (this is a normal behaviour).

So here's the code:

var contenu = db.Contenu.Where(c => c.IdContenu == idContenu).FirstOrDefault();
for (int i = 0; i< contenu.Tag.Count(); i++)
{
var tag = contenu.Tag.FirstOrDefault();

contenu.Tag.Remove(tag);               

db.SaveChanges();

}
Simple, yeah?

Hope this helps,

Raphaël

MVC 3 Helper for Hover Images...

Image
Hi Folks,

In this post I will talk about something that I haven't found on the internet, which is quite simple to do, which is a MVC Helper for creating a Hover image button...

If you are a professional developer and work in a team, you must have people which make the graphical stuff of your website, because most developers (like me) just don't like it, it's just a completely separate job.

This code assumes that you already understand MVC.

So here are my images, one named OK.png and the other OK_Hover.png, that are located on the Content/Images/Buttons folder. Now what I want do do is to use my helper (which appears on listing 1), to use it in my razor page, and that upon a click will redirect me to the corresponding view. So in my index.cstml I have: @using HoverImageHelper.Helpers;
@{
ViewBag.Title = "Index";
}Index
@Html.ImageButton("OK", "OK") So the first parameters corresponds to my PNG file, and the second to my action. Listing 1
///
///…

Excel Macro Tip

Hi All,

When I began writing macros in Excel 5.0 I didn't know how to find the last cell row, given a column...And that's useful, suppose you have a user form, and that you want to put the content of that userform on a sheet.So here's the tip: you just have to declare a long variable (you need this because on Excel 2003/2007 you have more than 32768 rows on a sheet).You can write it like this:

Dim lastRow as Long

LastRow=range("A65536").end(xlup).row

And that's it, folks!!!