【问题标题】:Adding MVC4 to Webforms Application - Routing Not Working将 MVC4 添加到 Webforms 应用程序 - 路由不起作用
【发布时间】: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 或配置ExtensionlessUrlHandler stackoverflow.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


【解决方案1】:

最终,我结束了使用现有网站并将其添加到 MVC 应用程序,而不是反过来。那时路由工作得很好。

【讨论】:

  • 这可能是我需要做的
  • 去吧。最后很容易做到。我绝对希望我没有浪费所有时间试图让它与我现有的应用程序一起工作。
【解决方案2】:

试试这个,

public static void RegisterRoutes(System.Web.Routing.RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    routes.MapRoute("Test", "{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional });
}

【讨论】:

  • IgnoreRoute 已经在里面了,我只是把相关部分放在我的问题中
【解决方案3】:

我认为您可能需要在 HomeController 中的 Index 方法中添加一个 id 参数。尽管它是可选的,但路由器正在寻找一种采用 id 的方法。

编辑:对不起,我不知道我在想什么。正如您引用的文章所证明的那样,这是错误的。

【讨论】:

    【解决方案4】:

    你能试试下面的路由规则吗?这在 MVC4 项目中对我有用。这定义了当您导航到某些 url 和端口(如 localhost:4897)时将加载的默认页面是什么

    public static void RegisterRoutes(RouteCollection routes)
    {
       routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    
       routes.MapRoute(
           name: "Default",
           url: "{controller}/{action}/{id}",
           defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
       );
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-18
      • 1970-01-01
      • 2016-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多