3

I am developing one new area of a fairly large web application using MVC3 and Razor, where the rest of the application is ASP.NET 4 Web Forms based. Can I include my MVC components in this application, and what do I have to do to achieve this?

3 Answers 3

4

Our experience has been that WebForms and MVC work well side-by-side in the same application, but not mixed. In other words, each page can either be WebForms or MVC, but it can't very easily include elements of both. The two main work-arounds I've found are:

  • Make all your MVC elements heavily AJAXified, so that they can be loaded dynamically via AJAX after the page loads.
  • Have two versions of your master pages, and anything else "common" to your web application, to make the MVC portions of your application have the same look and feel as the WebForms portions.

We use the latter approach.

Sign up to request clarification or add additional context in comments.

2 Comments

I think I'll also be using the two master file approach. I was getting scared of having to use an IFrame on my aspx master page to host my mvc 'content pages'.
@ProfK: Yeah, we use an iframe solution to integrate our application into another application written in Java, and there are a lot more pain points to that approach.
3

http://www.packtpub.com/article/mixing-asp.net-webforms-and-asp.net-mvc

That should get you on the right road. If it was me personally though, i'd add a new project to your solution that is MVC, you can then just configure a Virtual Directory in IIS /MVCApp

2 Comments

How would you config the root of the new project only (ie /mvc) if it was added to solution? Think this would be useful info. Andi G :) +1
Make sure you take the Virtual Directory url (e.g. /mvc) into consideration when you're planning your routes, as these are resolved relatively to the application, not the virtual directory.
0

I am using a class like this one to embed an MVC view into webforms page. Will try to do that in a reverse direction

public class HelperFactory
{
    private class FakeController : Controller
    {
    }

    private class FakeView : IView
    {
        public void Render( ViewContext viewContext, TextWriter writer )
        {
            throw new NotImplementedException();
        }
    }

    public static HtmlHelper<TModel> GetHelper<TModel>()
    {
        //HttpContextBase context = new HttpContext( HttpContext.Current );
        FakeController controllerBase = new FakeController();
        RouteData rd = new RouteData();
        rd.Values.Add( "controller", "Fake" );
        RequestContext requestContext = new RequestContext( new HttpContextWrapper( HttpContext.Current ), rd );
        ControllerContext fakeContext = new ControllerContext( requestContext, controllerBase );
        ViewDataDictionary vdd = new ViewDataDictionary();
        ViewContext viewCtx = new ViewContext( fakeContext, new FakeView(), vdd, new TempDataDictionary(), requestContext.HttpContext.Response.Output );

        return new HtmlHelper<TModel>( viewCtx, new ViewPage() );
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.