1

We have lot of pages in Web form and we don't want to migrate all of them in MVC, So we have decided to develop new pages into MVC.

I want to add MVC into my existing Web Form project.Can anyone provide more information about how to do this ?

How Can I use my Web Form masterpages into new MVC views?

There are solutions available but I am looking solution for Visual studio 2019(As settings are changed in vs 2019).

Ashutosh B Bodake
  • 1,304
  • 1
  • 19
  • 31

1 Answers1

2

The easiest way best way to start is to install Microsoft.AspNet.Mvc nuget package.

Create an area and then start building your controllers in that area. Things will just work just as they would in a normal MVC application.

Not using an area can be done as well. To do this you need to create a route.config class. Here is what it might look like. Note the line that tells it to avoid .aspx files.

 public static class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {              
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            routes.IgnoreRoute("{resource}.aspx/{*pathInfo}");  

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        }
    }

Then call the RegisterRoutes method from your Global.asax. It will look something like this

protected void Application_Start(object sender, EventArgs e)
{
    AreaRegistration.RegisterAllAreas();
    RouteConfig.RegisterRoutes(RouteTable.Routes);
   //other application_start code
}

If you want to start using some of the nice things MVC gives you like dependency injection you will need to create a startup file. I believe to create your startup file you need the nuget packages Microsoft.Owin and Microsoft.Owin.Host.SystemWeb. Add the startup file to the app in the same place as you would any mvc app and it will work as expected.

Using a web forms master page in an mvc layout is very hard. What I've found works well is to to put the code you need into various html helpers. Then these can be accessed from your web forms master page and your mvc layout page. Here is an example.

namespace MyHelpers
{
    public static class MyMasterPageNavigationHelper
    {
        public static MvcHtmlString GetNav(this HtmlHelper htmlHelper)
        {    
            return MvcHtmlString(GetNavigation());
        }


        public static string GetNavigation()
        {
            var a = new TagBuilder("a");
            a.Attributes.Add("href", "https://www.example.com");
            a.SetInnerText("example");    
            return a.ToString();    
         }    
    }
}

This is how you would call it for an Mvc layout page.

@using MyHelpers

@Html.GetNav()

This is how you would call it from a webforms page

<%@ Import Namespace="MyHelpers" %>

<%= MyMasterPageNavigationHelper.GetNavigation() %>

In both cases the html

<a href="https://example.com">example</a>

will be rendered. Hopefully you can see how this could grow to achieve the parts of your master page that you need.

Here are some links that I have found helpful with this topic.

This one explains how to introduce mvc using the area approach.

This one shows how you can start migrating a webforms page to an mvc page by posting to the web api. You might find this helpful if your masterpage has code in the postback.

Dave Barnett
  • 2,045
  • 2
  • 16
  • 32
  • Thanks Dave. This helps me to add MVC in my Web form project from Visual 2019. Now, I will try for master page. – Ashutosh B Bodake Aug 29 '19 at 07:10
  • Hi Dave, Any other way to pass webform master page to MVC view. Or I must have to create new _Layout.cshtml which is same as web form master page and use this _layout.cshtml in MVC view pages. – Ashutosh B Bodake Aug 29 '19 at 17:27
  • I think that you will need to use the _layout.cshtml. This answer [explains the reasons why](https://stackoverflow.com/a/30708698/3773983). It basically says MVC can't handle the viewstate. So my advice is to use master for webforms, _layout.cshtml for mvc and where possible get master page and _layout.cshtml to share html helpers to avoid duplication. – Dave Barnett Aug 29 '19 at 19:33