Articoli per la categoria '.NET'

JsonResult e il browser che chiede il salvataggio del json

Se una volta fatta una richiesta ajax, il browser vi chiede di salvare il file che contiene la risposta in formato json…

return new JsonResult
 {
     Data =  (pageId + "+" + url).ToJson() ,
     ContentType = "text/json",
     JsonRequestBehavior = JsonRequestBehavior.DenyGet
 };

Inltre, nella chiamata ajax, è opportuno interpretare la risposta nel seguente modo:

onComplete: function(id, fileName, responseJSON) {
var jsonData = eval("(" + responseJSON + ")");
var tmpris = jsonData.split("+"); //per esempio
}

Il metodo to .ToJson è stato postato in qualche articolo precedente.

Json e carattere “+”

Basta utilizzare la funzione javascript

encodeURIComponent

Calcolare l’Hash di una lista (Extension Method)

public static class Extensions
{
	public static int CalculateHash<T>(this List<T> myList) where T : IEquatable<T>
	{
	if (myList.Count == 0) return 0;
		unchecked
		{
			return myList.Aggregate(0, (current, v) => (current ^ 397)*v.GetHashCode());
		}
	}
}

System.ApplicationException : For property ‘CreationDate’ expected ’19/07/2010 17:27:17′ of type ‘System.DateTime’ but got ’19/07/2010 17:27:17′ of type ‘System.DateTime’

Se eseguendo un test con NUnit si riceve questo errore è probabile che bisogna utilizzare DateTime.Today invece di DateTime.Now.

Test con Rhino Mocks

MockRepository mockRepository = new MockRepository();
var logRepository = mockRepository.StrictMock<ILogRepository>();
//la classe da testare ha un construttore che accetta una interfaccia
LogController sut = new LogController(logRepository);
 
string returned =....
 
//Mock metodo GetLog; mi aspetto che venga chiamato una sola volta
logRepository.Expect(t => t.GetLog(eventualiParametri)).Return(returned ).Repeat.Once();
mockRepository.ReplayAll();
 
string expected = sut.MetodoDaTestare();
 
Assert.AreEqual(expected, actual.Data);
 
logRepository.VerifyAllExpectations();

Rendere un assembly visibile ad un altro

Nel file AssemblyInfo.cs dell’assemby che deve essere visibile inserire la seguente riga di codice:

[assembly: InternalsVisibleTo("MioNamespace.NomeAssembly")]

Adesso MioNamespace.NomeAssembly dovrebbe poter “vedere” i membri dichiarati internal.