MVC and ASP.Net website in one solution

Hi

I wanted to know how to reference my ASP.Net aspx page in my MVC project.

I know how to do it with Areas and a seperate MVC Project. BUT when I add an ASP.Net Application I’m unsure how to route to the Default.aspx page.

I followed:

http://dotnetslackers.com/articles/aspnet/Storing-ASP-NET-MVC-Controllers-Views-in-separate-assemblies.aspx

The core and setup pages relate to the MVC but how do I reference my Default.aspx page. I get a 404 when I type in localhost/Default.aspx

I did add the default aspx page to the bin folder of the mvc. But not sure if I should do a Core folder and Setup as in article for the ASP.Net application as it doesn’t work with Controllers and Views?

Core folder ViewEngine:

public class CMSViewEngine : WebFormViewEngine
{
    public CMSViewEngine()
    {
        MasterLocationFormats = new string[]
        {
            "~/bin/Views/{1}/{0}.master",  
            "~/bin/Views/Shared/{0}.master"
        };
        ViewLocationFormats = new string[]
        {
            "~/bin/Views/{1}/{0}.aspx",  
            "~/bin/Views/{1}/{0}.ascx",  
            "~/bin/Views/Shared/{0}.aspx",  
            "~/bin/Views/Shared/{0}.ascx"
        };
    }
}

Setup:

public class CMSSetup
{
    public static void Setup()
    {
        SetupControllers();
        SetupViewEngine();
    }
    private static void SetupViewEngine()
    {
        ViewEngines.Engines.Add(new CMSViewEngine());
    }
    private static void SetupControllers()
    {
        ControllerBuilder.Current.DefaultNamespaces.Add("CMS.Controllers");
    }
    public static void SetupRoutes(RouteCollection routes)
    {
    }
}

Global asax:

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

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional }, new[]{"HCCWebTemplate.Controllers"}
            );

        }

        /// <summary>
        /// Application Start where areas are registered
        /// </summary>
        protected void Application_Start()
        {
            CMSSetup.Setup();


            AreaRegistration.RegisterAllAreas();

            RegisterRoutes(RouteTable.Routes);
        }

They reason why is that my MVC is a framework that will be used by other developers and some of them only need to develop Single Page Applications. So only one web page to slot into my framework and they are more comfortable with ASP.Net Application Web Pages.

Hope someone can help me.

Thanks

 

Hi sunettew1,

What’s the version of your MVC project?

For that link that you provide, it uses the MVC 2. In the MVC 2 project, it uses
aspx files as views.

For the higher version of MVC project, it uses cshtml files as views, to enable web forms in the asp.net mvc project, you just need to add this code below to ignore the web forms request.

routes.IgnoreRoute("{resource}.aspx/{*pathInfo}");

The files in the view folder, we can’t access it directly.

Best Regards

Starain Chen

Hi

I use MVC 2. How do I reference the website’s pages because there is no controller?

Thanks

Hi sunettew1,

You could try to use MapPageRoute method or access it directly.

# RouteCollection.MapPageRoute Method

http://msdn.microsoft.com/en-us/library/system.web.routing.routecollection.mappageroute(v=vs.100).aspx

Best Regards

Starain

Leave a Reply