Check the MetaModel Existence while Running a Dynamic Data Application

In the web application that I’m developing using Dynamic Data, I set the DataContext at runtime. The application might bind different contexts depending on many variables. I usually test it on two different browsers, and, because of the session, I need to pass the same procedure twice. The second time, the DataContext is already set, so I don’t need to register the context anymore. The simple condition I use: if (System.Web.DynamicData.MetaModel.Default == null) { // Register context and routes ... } Otherwise I would get this exception: ...

June 18, 2008 · 1 min · Andrea Azzola

Set the ConnectionString at Runtime with DynamicData

Note (2025): This article dates back to 2008 and refers to ASP.NET Dynamic Data, which is legacy today. Concepts can still be useful historically, but for modern .NET consider newer frameworks and data‑binding approaches. The following snippet customizes the .NET ConnectionString at runtime in a Dynamic Data application. Edit Global.asax and modify the RegisterRoutes(RouteCollection routes) method. Instead of: model.RegisterContext(typeof(XYZDataContext), new ContextConfiguration() { ScaffoldAllTables = true }); Use: model.RegisterContext( () => new XYZDataContext(connectionString), new ContextConfiguration { ScaffoldAllTables = true } ); Where connectionString is your string value. You can customize the RegisterRoutes parameter collection without compromising application functionality. ...

June 16, 2008 · 1 min · Andrea Azzola

Consuming .NET Webservices Using PHP and NuSoap

In my scenario a .NET webservice offers particular encrypting capabilities. I need to consume the webservice with PHP by sending my plain string to get an encrypted one in return. The .NET webservice is working properly. I found the NuSoap library, and adopted it. Here the inclusion: require_once('lib/nusoap.php'); And here the code: // requires web service's specifications aka wsdl // $parameters: // requires following the array 'notation' // array('parameters' => (array('text' => 'Hello World!!!'))); // that equals to Hello World!!! // $proxy (->host, ->port, ->username, ->password) // optional, uses a proxy server function consume_webservice($wsdl, $method, $parameters, $debug = false, $proxy = null) { if ($proxy != null) $proxy->host = $proxy->port = $proxy->username = $proxy->password = false; $client = new soapclient($wsdl, true, $proxy->host, $proxy->port, $proxy->username, $proxy->password); $err = $client->getError(); if ($err) echo 'Constructor error' . $err; $result = $client->call($method, $parameters); // check for a fault if ($client->fault) { echo 'Fault'; print_r($result); } else { // check for errors $err = $client->getError(); if ($err) echo 'Error' . $err; } if ($debug) { echo 'Request' . htmlspecialchars($client->request, ENT_QUOTES); echo 'Response' . htmlspecialchars($client->response, ENT_QUOTES); echo 'Debug' . htmlspecialchars($client->debug_str, ENT_QUOTES); } return $result; } This approach works only with SOAP webservices. Please note that .NET web services include all response content into a “parameters” section by design. ...

October 23, 2007 · 1 min · Andrea Azzola

Issues With AJAX and a Custom HttpModule

My web app needs to catch every non‑existent path as a search string, because I’m implementing an HttpModule that handles URLs in an SEO‑friendly way, e.g.: http://contoso.com/search-string But the Microsoft AJAX ScriptModule interfered, causing runtime errors like “AJAX Framework failed to load…”. The fix was to register my module after the ScriptModule and selectively bypass AJAX requests. <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, ..." /> <add name="MyModule" type="MyModule" /> using System; using System.Linq; using System.Web; public class MyModule : IHttpModule { public void Init(HttpApplication context) { context.PostResolveRequestCache += Application_OnAfterProcess; } private void Application_OnAfterProcess(object source, EventArgs e) { var application = (HttpApplication)source; var context = application.Context; if (context.Request.Headers["x-microsoftajax"] == null) { if (!System.IO.File.Exists(application.Request.PhysicalPath) && !application.Request.Url.ToString().Contains(".axd") && !application.Request.Url.ToString().Contains(".asmx")) { string newUrl = "~/Search.aspx?q=" + context.Server.UrlEncode(application.Request.Url.Segments.Last()); // other logic here… context.RewritePath(newUrl); } } } public void Dispose() { } } This runs at PostResolveRequestCache and, when the special x-microsoftajax header is absent, it rewrites unknown paths to your search page while letting AJAX handlers (*.axd, *.asmx) pass through normally. ...

June 18, 2005 · 1 min · Andrea Azzola