nam
nam

Reputation: 23868

How to do routing with both WebForms and MVC in a VS2013 web project

I have converted a website project from VS2010 to a Web application project in VS2013. Now I want to integrate a VS2012 MVC4 project to this VS2013 project. So I created an MVC Area, called Test, in this new VS2013 project. Hence a new class was created in Test area folder as follows:

public class TestAreaRegistration : AreaRegistration 
    {
        public override string AreaName 
        {
            get 
            {
                return "Test";
            }
        }

        public override void RegisterArea(AreaRegistrationContext context) 
        {
            context.MapRoute(
                "Test_default",
                "Test/{controller}/{action}/{id}",
                new { action = "Index", id = UrlParameter.Optional }
            );
        }
    }

Inside the Controller folder of the "Test" area, I created a HomeController with Index action method - and a corresponding view "Index.cshtml".

When I run the app, it by default opens the Default.aspx page of the WebFomrms. And if I open the app through http://localhost:1234/WebAppName/Home/Index, it opens the Index.cshtml view. I like all of this so far.

How do I do routing inside this new app to navigate between webform and an MVC view. For example my WebForm home page has a left navigation bar where I have links to some WebForm pages that a user can navigate to with no problems. How do I add a link for a MVC veiw to this WebForm left navigation bar so that a user can navigate back and forth between webform pages and MVC views.

My project is targeting .NET 4.5.1.

Upvotes: 0

Views: 1169

Answers (1)

Jason Roell
Jason Roell

Reputation: 6819

You can just use a Response.Redirect from your WebForms code with the right URL which gets routed to your controller in question, I guess in your case, that is:

Response.Redirect("/Home/Index");

Or even better you could make an extension method for HtmlHelper that handles your routing:

    public static class ExtensionMethods
    {
        public static MvcHtmlString WebFormActionLink(this HtmlHelper htmlHelper, string linkText, string ruoteName, object routeValues)
        {
            var helper = new UrlHelper(htmlHelper.ViewContext.RequestContext);

            var anchor = new TagBuilder("a");
            anchor.Attributes["href"] = helper.RouteUrl(routeName, routeValues);
            anchor.SetInnerText(linkText);
            return MvcHtmlString.Create(anchor.ToString());
        }
    }

Your Routes:

public static void RegisterRoutes(RouteCollection routes)
{

    routes.MapPageRoute(
          "Webforms-Route", // Route name
          // put your webforms routing here
         );

        routes.MapRoute(
            "MVC-Route", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional } 
            );
    }

You would then just pass in your route to your HtmlHelper and you will get the correct route.

Upvotes: 0

Related Questions