【发布时间】:2014-12-13 12:17:30
【问题描述】:
我正在尝试将 MVC 4 添加到现有的网络表单项目中。我遵循了这个指南:https://www.packtpub.com/books/content/mixing-aspnet-webforms-and-aspnet-mvc
添加到 web.config 后:
<add assembly="System.Core, Version=3.5.0.0, Culture=neutral,PublicKeyToken=B77A5C561934E089"/>
<add assembly="System.Web.Mvc, Version=4.0.0.1, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add assembly="System.Web.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add assembly="System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add assembly="System.Web.WebPages, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
我在Controllers文件夹中做了一个Controller(HomeController.cs):
using System.Web.Mvc;
namespace MixingBothWorldsExample.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
ViewData["Message"] = "This is ASP.NET MVC!";
return View();
}
}
}
然后我在 Views/Home/index.aspx 添加了我的视图:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="index.aspx.cs" Inherits="MixingBothWorldsExample.Views.Home.Index" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title></title>
</head>
<body>
<div>
<h1><%=Html.Encode(ViewData["Message"]) %></h1>
</div>
</body>
然后添加我的路线:
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
RegisterRoutes(System.Web.Routing.RouteTable.Routes);
}
public static void RegisterRoutes(System.Web.Routing.RouteCollection routes)
{
routes.IgnoreRoute("{resource}.aspx/{*pathInfo}");
routes.MapRoute("Test", "{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional });
}
出于某种原因,我现有的 webforms 项目中的所有内容都可以正常工作(包括 PageRoutes),但是 /home/index 给了我一个 404,这意味着它甚至没有路由到控制器。
有人有什么想法吗?
【问题讨论】:
-
尝试
runAllManagedModulesForAllRequests或配置ExtensionlessUrlHandlerstackoverflow.com/questions/12495346/… -
运行 wireshark 跟踪或打开开发工具 (F12) 并告诉我们发布的数据是什么样的。我们可以从那里去......
-
仅从 ASP.NET 4 开始将 URL 路由功能添加到 Web 窗体项目中
-
我试用了路由调试器。在 Route Data 表中,它列出了以下 KVP:controller=home 和 action=index 它还说 mvc 路由与当前请求匹配,但我仍然得到 404
标签: c# asp.net asp.net-mvc webforms asp.net-routing