asp.net-MVC和Web表单共存(asp.net-mvc and webforms co-ex

2019-10-18 16:49发布

我知道我已经问过这个问题,我知道有很多资源在那里描述的技术,但我不能为我的生命做出来。

我开始通过创建一个简单的WebForms应用程序。

然后我加入了视图和控制器的文件夹。 然后我添加的家庭和共享文件夹的意见,并增加了Index.aspx的和的Site.Master文件。

我创建了HomeController.cs文件。

然后我做了更改web.config文件,并添加web.config中到的意见文件夹中。

那么我所做的更改,以在Global.asax页。

整个事情编译,路线会得到注册,但我根本无法打开主页/索引页。

我总是得到“HTTP错误404 - 找不到”

有没有人真正能够做到这一点? 哦,我们在这里使用了IIS6。

下面的代码

全球

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>

家庭控制器

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();
    }
}

首页查看

<%@ 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>

Answer 1:

我会走另一条路,并创建一个MVC应用程序,然后添加你需要的web表单。 作为默认的简单得多的MVC应用程序都已经为你设置的neccessary位。

您需要创建一个单独的文件夹在您的web表单会住。 然后你就可以包括/需要从路由引擎排除的文件夹。



Answer 2:

原来,该网站需要的是一个Web应用程序。

我还需要添加以下到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"/>

感谢和+1到所有试图帮助。



Answer 3:

你设置了通配符映射?

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



文章来源: asp.net-mvc and webforms co-existing