Monday, April 25, 2011

Getting ASP.NET Mvc and Web Forms to work together

Hello,

Sorry if this as already been asked.

I am currently working on a small feature and am trying to implement the ASP.NET Mvc framework into my current Web Forms web application. I have been following the Professional ASP.NET 3.5 Mvc Chapter 13 pdf document that I recently found on stack**overflow** to get Web Forms and Mvc to work together. I have completed all three steps:

  1. Added the references to the libraries System.Web.Mvc, System.Web.Routing, and System.Web.Abstractions

  2. Added the two directories to my web application: controllers and views

  3. Updated the web.config to load the three assemblies mentioned in step one and registered the UrlRoutingModule HttpModule. I also added the initial routing settings to my Global.asax file

    RouteTable.Routes.MapRoute( "Default", "{controller}/{action}/{id}", new { controller = "Support", action = "Index", id = "" } );

Once I try and run my newly created page following the ASP.NET Mvc framework I get a NullReferenceException on the following piece of code:

<%= Html.Encode(ViewData["Message"]) %>

In my controller I have ViewData["Message"] being set to "Message!" just as a test. Have I missed something setting my web application to work with ASP.NET Mvc?

Any help would be greatly appreciated.

Thanks,

From stackoverflow
  • From playing with the early betas, until today, I find it easier to create a new MVC application and "import" my exsiting files into the solution than it is to get all of the settings right in an existing application. I see no reason to get hot and heavy over setting up plumbing I can have written for me from a template. Perhaps I am just too lazy.

    I find that I can get "legacy ASP.NET" web forms working fine with MVC. The only dink to the whole project is getting the app routed to my default.aspx to begin with. Much easier than running through multiple steps to include MVC.

    I know this is not a direct answer to your question, but I think trying it will solve your problem, as well. Another benefit is you leave the old "legacy" source in case a bug fix comes in. But, then, you should be under source control anyway.

0 comments:

Post a Comment