wtorek, 31 marca 2009

Create custom ActionInvoker

W skrócie ControllerActionInvoker jest wywoływany przez kontroler, aby wykonać akcję.
Wiecej informacji na ten temat tutaj

Tworzymy własny ActionInvoker:



/// <summary>
/// Custom Action Invoker. The ControllerActionInvoker is responsible for creating the parameters that are passed to a controller action.
/// </summary>
public class ContextActionInvoker : ControllerActionInvoker
{

public ContextActionInvoker(ControllerContext controllerContext)
: base()
{

}

/// <summary>
/// Invokes the specified action. Override to log browsing.
/// </summary>
/// <param name="controllerContext">The controller context.</param>
/// <param name="actionName">The name of the action.</param>
/// <returns></returns>
public override bool InvokeAction(ControllerContext controllerContext, string actionName)
{
HttpContextBase context = controllerContext.HttpContext;

//Do sth

return base.InvokeAction(controllerContext, actionName);

}

}


Tworzymy własny ControllerFactory. Przeładowywujemy funkcję GetControllerInstance, w której przypisujemy instancję własnego invoker'a.



/// <summary>
/// Custom Controller Factory
/// </summary>
public class ContextControllerFactory : DefaultControllerFactory
{

/// <summary>
/// Gets the controller instance. Override to put down custom ActionInvoker to ActionInvoker
/// </summary>
/// <param name="controllerType">Type of the controller.</param>
/// <returns>A reference to the controller.</returns>
protected override IController GetControllerInstance(Type controllerType)
{

IController controller = base.GetControllerInstance(controllerType);

Controller contextController = controller as Controller;

if (contextController != null)
{

var context = new ControllerContext(this.RequestContext, contextController);

contextController.ActionInvoker = new ContextActionInvoker(context);

}

return controller;

}

}


CustomControllerFactory rejestrujemy w global.asax.cs



protected void Application_Start()
{
//Register new Controller Factory
ControllerBuilder.Current.SetControllerFactory(typeof(ContextControllerFactory));

RegisterRoutes(RouteTable.Routes);

}

środa, 7 stycznia 2009

Generowanie skryptów bazy danych (Generating database script)

Jeżeli chcesz wygenerować skrypt bazy danych zawierający schemę wraz z danymi możesz użyć programu Microsoft SQL Server Database Publishing Wizard. Po zainstalowaniu aplikacji możliwe jest generowanie skryptu wprost z Visual Studio. Jak się tą aplikacją posługiwać opisuje na swoim blogu Scott Gu

środa, 10 grudnia 2008

Ukrywanie obszaru div przy pomocy JavaScript (Show and hide div tag in JavaScript)

Ukrywanie i pokazywanie bloków div na stronach HTML

Wystarczy w nagłówku strony dodać kod:



<script language="javascript" type="text/javascript">


function hidediv() {
if (document.getElementById) { // DOM3 = IE5, NS6
document.getElementById('hideshow').style.visibility = 'hidden';
}
else {
if (document.layers) { // Netscape 4
document.hideshow.visibility = 'hidden';
}
else { // IE 4
document.all.hideshow.style.visibility = 'hidden';
}
}
}

function showdiv() {
if (document.getElementById) { // DOM3 = IE5, NS6
document.getElementById('hideshow').style.visibility = 'visible';
}
else {
if (document.layers) { // Netscape 4
document.hideshow.visibility = 'visible';
}
else { // IE 4
document.all.hideshow.style.visibility = 'visible';
}
}
}
</script>


Teraz pozostaje tylko wywoływać odpowiednią funkcję np.:


<asp:Button ID="btn" runat="server" Text="Button" OnClientClick="showdiv()" />

piątek, 28 listopada 2008

Null Http.Context in WCF

Problem: Obiekt Http.Context w sewisie WCF jest null.

Rozwiązanie:
Należy dodać do web.config:


<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled=”true/>
</system.serviceModel>


I do klasy WCF nagłowek:


[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
Public class Sevice:Iservice
{}

Więcej informacji na ten temat można znaleźć pod tym adresem: http://msdn.microsoft.com/en-us/library/aa702682.aspx