- Public Module MyUrlExtensions
- <Extension()> _
- Public Function UrlOriginal(ByVal request As HttpRequestBase) As Uri
- Dim hostHeader As String = request.Headers(“host”)
- Return New Uri(String.Format(“{0}://{1}{2}“, request.Url.Scheme, hostHeader, request.RawUrl))
- End Function
- End Module
Author Archives: george2giga
Losing project references on build with Visual Studio 2010
While building a Vb.net Windows service the references to a couple of other projects within the same solution seemed lost. While the references where still there, in the project properties the framework target was set to .Net Framwork 4 Client Profile. Change it to .Net Framwork 4 and you’ll get your references back !
String.Format in Jquery
- postcodeSelections += $.validator.format('<select size="4" name="{0}" id="{1}" tabindex="10" class="dropdownPostcodes">', postcodeSelectionsIdentifier, postcodeSelectionsIdentifier);
Scaffhold views/controllers within n-tier solutions. EF Code first flavour.
I’ve been struggling for a while this morning, trying to use one of the most popular MVC tools features: the ability to generate a controller with the CRUD (Create, Read, Update, Delete) actions over a model. The generator will create the different views as well. Or better.. it should =) Everything works smoothly when you got one project only within your solution , including models, dal etc.
While if your app is layered into several projects, here’s a couple of things you should keep in mind in order to get the scaffholding working:
- The project containing the model you want to scaffhold must include the configuration file with the connection string for your context.
- The web project (from where you will create the scaffholded controller) not only it must have the config file with the connection string. The connectionstrings can’t be in separated config files (configSources)! Or you will end up getting this : The type initializer for ‘System.Data.Entity.Internal.AppConfig’ threw an exception.
Last thing to keep in mind is to expose the DbSets instead of IDbSets in your DbContext or the code generator will not work. Happy scaffholding to everyone!
Easy Asynch emails in MVC 3
Here’s a workaround if you’re facing issues implementing the Smtp.SendAsync() method. It doesn’t require any change in your actions or controllers (like changing them to be AsycController).
If you got one, you can implement the below in your service layer. The original code is in Vb.Net, like the project I am working on…sorry . Btw I’ve converted with one of the several tools online.
{
object smtp = new SmtpClient {
Host = mailManagerSettings.Host,
Port = mailManagerSettings.Port,
EnableSsl = mailManagerSettings.EnableSSL,
DeliveryMethod = mailManagerSettings.DeliveryMethod,
Credentials = new NetworkCredential(mailManagerSettings.Username, mailManagerSettings.Password)
};
TriggerMailDelegate triggerHandler = new TriggerMailDelegate(smtp.Send);
AsyncCallback sendCompleteCallback = new AsyncCallback(SendEmailCbk);
// We send an email to each user in the ToAddress list
foreach ( address in mailManagerSettings.ToAddress) {
object message = new MailMessage(mailManagerSettings.Username, address) {
Subject = mailManagerSettings.Subject,
Body = mailManagerSettings.Body,
IsBodyHtml = mailManagerSettings.IsBodyHtml
};
foreach ( attachment in _attachments) {
message.Attachments.Add(new Attachment(attachment));
}
//Asynch send
if ((isAsynch)) {
triggerHandler.BeginInvoke(message, sendCompleteCallback, triggerHandler);
} else {
smtp.Send(message);
}
}
}
public void AddAttachment(string path)
{
_attachments.Add(path);
}
private delegate void TriggerMailDelegate(System.Net.Mail.MailMessage message);
private static void SendEmailCbk(IAsyncResult result)
{
TriggerMailDelegate sendHandler = (TriggerMailDelegate)result.AsyncState;
//DO SOMETHING WHEN EMAIL IS SENT
sendHandler.EndInvoke(result);
}
Sql server enable tracing
SELECT * FROM ::fn_trace_gettable (‘c:\giorgio\tracesql.trc’, default) where CPU > 10000
Get database Id in Sql Server
Just run this:
SELECT db_id('databasename')
Moq a Vb.net Sub
While mocking a VB method I was getting the following error :
‘Public Function Setup(expression As System.Linq.Expressions.Expression(Of System.Action(Of EmailSender.IMailManager))) As Moq.Language.Flow.ISetup(Of EmailSender.IMailManager)’: Expression does not produce a value
Here’s the moq setup :
mailManager.Setup(Function(x) x.SendEmail(It.IsAny(Of MailManagerSettings))).Verifiable()Because the SendEmail method is a Sub instead of a Function.
Let's replace the Setup target Function with a Sub and this will complile. (Sub lambda support is quite new though.)
Troubleshooting IIS 7.5 on Window 7
I’ve just installed IIS 7.5 on a machine with Windows 7. Of course the website I deployed didn’t work on the first attempt, here’s a few steps to fix the missing (or wrong) settings on the webserver. Before starting ensure the url you enter on the browser points to the deployed website. Main things to check :
- The host file under C:\Windows\System32\drivers\etc should have an entry for the website deployed.
- Assign the same url to the host name of your website under IIS.
* Ensure you enter the site url including the port, es http://www.dev.mywebsite.com:8099
Once all the correct urls are been set on both iis and the host file, try access the site.
The same website working perfectly within the Visual Studio webserver, now on IIS, returns this error when trying to browse it:
HTTP Error 500.19 – Internal Server Error
Description: The requested page cannot be accessed because the related configuration data for the page is invalid.
Odd because the web.config haven’t changed. To fix it edit the applicationHost.config in the c:\Windows\system32\inetsrv\config folder by changing the following key values:
- <section name="handlers" overrideModeDefault="Deny" />
- <section name="modules" allowDefinition="MachineToApplication" overrideModeDefault="Deny" />
JQuery filtering an array of objects using a condition
The equivalent of a Linq.Where() in JQuery:
var elem = $.grep(postcodeResults, function (n) { return n.Udprn == selectedOption; });
Next step will be using linqjs


