asp.net-mvc and webforms co-existing

2019-08-01 23:48发布

问题:

I know I've asked this question before, and I know there are plenty of resources out there describing the technique but I cannot for the life of me work it out.

I began by creating a simple WebForms application.

I then added the Views and Controllers folders. I then added home and shared folders to the views and added an Index.aspx and a site.master file.

I created the HomeController.cs file.

I then made the changes to the web.config and added the web.config into the views folder.

I then made the changes to the global.asax page.

The whole thing compiles, routes appear to get registered but I simply cannot open the Home/Index page.

I always get "HTTP Error 404 - Not Found"

Has anyone actually managed to do this? Oh and we're using IIS6 here.

CODE BELOW

Global

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

    routes.MapRoute(
        "Default",                                              // Route name
        "{controller}.mvc/{action}/{id}",                           // URL with parameters
        new { action = "Index", id = "" }  // Parameter defaults
    );

    routes.MapRoute(
        "Root",
        "",
        new { controller = "Home", action = "Index", id = "" }
      );

}

Web.Config

<compilation debug="true">
            <assemblies>
                <add assembly="System.Web.Abstractions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
                <add assembly="System.Web.Mvc, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
                <add assembly="System.Web.Routing, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>


        <pages>
            <controls>
                <add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
                <add tagPrefix="asp" namespace="System.Web.UI.WebControls" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
            </controls>
            <namespaces>
                <add namespace="System.Web.Mvc"/>
                <add namespace="System.Web.Mvc.Html"/>
                <add namespace="System.Web.Routing"/>
                <add namespace="System.Web.Mvc.Ajax"/>
                <add namespace="System.Linq"/>
                <add namespace="System.Collections.Generic"/>
            </namespaces>
        </pages>


        <httpModules>
            <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
            <add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web.Routing, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        </httpModules>

Home Controller

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

[HandleError]
public class HomeController : Controller
{

    public ActionResult Index()
    {
        return View();
    }

    public ActionResult About()
    {
        return View();
    }
}

Home View

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/MVCSite.master" Inherits="System.Web.Mvc.ViewPage"%>

<script runat="server">

</script>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">

    MVC Home Page

    <%= Html.TextBox("txtTest") %>
    <%= Html.ActionLink("About Page", "About") %>

</asp:Content>

回答1:

I would go the other way and create an MVC application and then add the webforms that you needed. much simpler as the default MVC application has all the neccessary bits already set up for you.

You would need to create a seperate folder where your webforms would live. Then you can include/exclude the folder from the routing engine as needed.



回答2:

Turns out that the web site needed to be a web application.

I also needed to add the following into the web.config;

<add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web.Routing, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>

            <remove name="UrlRoutingModule"/>
            <add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web.Routing"/>

Thanks and +1 to all that tried to help.



回答3:

Did you set up the wildcard mappings?

http://haacked.com/archive/2008/11/26/asp.net-mvc-on-iis-6-walkthrough.aspx