Posts tagged ‘ASP.NET MVC’

Dynamically Loading Partial Views with the Spark View Engine

Loading a partial view is easy with Spark. There are basically two ways:

<use file="MyPartialView" />

And if you have name your files using the _MyPartialView.spark convention:

<MyPartialView />

Now what if you don’t know the name of the partial file until runtime and need to dynamically render the view? As far as I know, there is no way of doing this in Spark. How you can get around this is by doing some inline code using ASP.NET MVCs RenderPartial method.

#Html.RenderPartial( myPartialViewVariable );

This is assuming myPartialViewVariable is a string variable with the name of the view that needs to be rendered.

Update:

When you use ASP.NET’s Html.RenderPartial, your data isn’t automatically passed along to the spark view. To pass your data along, use the ViewData dictionary, like you would in a controller.

<ul>
    <li each="var item in items">
        #ViewData["item"] = item;
        #Html.RenderPartial( "path/to/" + item.Name );
    </li>
</ul>

In the partial view, use the <viewdata /> spark attribute like you normally would in a spark file.

<viewdata item="Item" />

ASP.NET MVC, Ninject.Web.Mvc and 404’s

I went about trying to handle 404 errors on my ASP.NET MVC application by following code I found here.

protected void Application_Error( object sender, EventArgs e )
{
    var exception = Server.GetLastError();

    Response.Clear();

    var httpException = exception as HttpException;

    var routeData = new RouteData();
    routeData.Values.Add( "controller", "Error" );

    if( httpException == null )
    {
        routeData.Values.Add( "action", "Index" );
    }
    else //It's an Http Exception, Let's handle it.
    {
        switch( httpException.GetHttpCode() )
        {
            case 404:
                // Page not found.
                routeData.Values.Add( "action", "HttpError404" );
                break;
            case 500:
                // Server error.
                routeData.Values.Add( "action", "HttpError500" );
                break;

            // Here you can handle Views to other error codes.
            // I choose a General error template
            default:
                routeData.Values.Add( "action", "General" );
                break;
        }
    }

    // Pass exception details to the target error View.
    routeData.Values.Add( "error", exception );

    // Clear the error on server.
    Server.ClearError();

    // Call target Controller and pass the routeData.
    IController errorController = new ErrorController();
    errorController.Execute( new RequestContext( new HttpContextWrapper( Context ), routeData ) );
}

Although, this didn’t work for me because I’m using Ninject.Web.Mvc. Instead, I get this error:

The IControllerFactory ‘Ninject.Web.Mvc.NinjectControllerFactory’ did not return a controller for a controller named ‘blah’.

The NinjectControllerFactory.CreateController( RequestContext requestContext, string controllerName ) method is returning null when it can’t find a controller, when it should be throwing a 404 HttpException.

There apparently is a patch waiting to be merged into trunk for this, but until then, you can use this change to the controller factory. The factory now inherits from DefaultControllerFactory, CreateController and ReleaseController are now overrides, and a test for a null controller will call base.CreateController.

using System;
using System.Globalization;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace Ninject.Web.Mvc
{
    /// <summary>
    /// A controller factory that creates <see cref="IController"/>s via Ninject.
    /// </summary>
    public class NinjectControllerFactory : DefaultControllerFactory
    {
        /// <summary>
        /// Gets the kernel that will be used to create controllers.
        /// </summary>
        public IKernel Kernel { get; private set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="NinjectControllerFactory"/> class.
        /// </summary>
        /// <param name="kernel">The kernel that should be used to create controllers.</param>
        public NinjectControllerFactory(IKernel kernel)
        {
            Kernel = kernel;
        }

        /// <summary>
        /// Creates the controller with the specified name.
        /// </summary>
        /// <param name="requestContext">The request context.</param>
        /// <param name="controllerName">Name of the controller.</param>
        /// <returns>The created controller.</returns>
        public override IController CreateController(RequestContext requestContext, string controllerName)
        {
            var controller = Kernel.TryGet<IController>(controllerName.ToLowerInvariant());

            if(controller == null)
                return base.CreateController(requestContext, controllerName);

            var standardController = controller as Controller;

            if(standardController != null)
                standardController.ActionInvoker = new NinjectActionInvoker(Kernel);

            return controller;
        }

        /// <summary>
        /// Releases the specified controller.
        /// </summary>
        /// <param name="controller">The controller to release.</param>
        public override void ReleaseController(IController controller) { }
    }
}

DevConnections – ASP.NET MVC Framework

This talk was given by Scott Hanselman and Eilon Lipton. Scott is the product manager in charge of the MVC framework, and Eilon the developer creating it.

This talk was mostly code examples.

  • MVC FX is a new alternative to WebForms.
  • Pluggable with IOC containers.
  • Uses the Front Controller pattern.
  • Only runs on .NET 3.5.
  • It will be a new project type. It creates a solution with 2 projects in it. The website project, and a test project for the site.

For more information on the framework, check out Scott Hanselman’s post with a talk from himself and Scott Guthrie.

MVC for ASP.NET Video

Scott Hanselman has posted a video of the full talk that ScottGu gave on MVC for ASP.NET.

I’m even more excited after watching the video.

MVC for ASP.NET

Microsoft is finally going to release a framework for creating web pages using the MVC (Model View Controller) pattern. It will be integrated into the core and not sitting on top of System.Web.UI.Page. Aspx pages were created to behave just like Windows Forms, rather than how the web actually works.

This will be a nice change, and long overdue.