【问题标题】:Routing to an aspx page on a project with webforms and MVC使用 webforms 和 MVC 路由到项目中的 aspx 页面
【发布时间】:2014-07-08 20:45:57
【问题描述】:

我已将 MVC 添加到我一直在处理的现有 webforms 项目中,因为我希望慢慢迁移该项目,但是,在更改期间我需要能够访问这两者,但拥有它默认为 aspx 页面。

在注册路由时,我目前有这个:

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

        routes.MapPageRoute("Backup", "{*anything}", "~/Default.aspx");

        routes.MapRoute(
            "MVC", 
            "{controller}/{action}/{id}", 
            new { controller = "Test", action = "Index", id = 1 }
        );
    }

但是,当我这样设置时,即使我输入了一个我知道是指控制器/动作组合的 URL(例如,最后一行中的测试和索引),它仍然会将用户重定向到Default.aspx 页面。

我已尝试将其更改为以下内容:

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

        routes.MapRoute(
            "MVC",
            "{controller}/{action}/{id}",
            new { controller = "Test", action = "Index", id = 1 }
        );

        routes.MapPageRoute("Backup", "{*anything}", "~/Default.aspx");
    }

这里,用户可以指定一个页面或者指定一个控制器/动作,但是如果他们不包含任何控制器/aspx页面,它默认为默认的MVC路由,而我需要它默认为webform Default.aspx .

我将如何解决这种情况,以便如果没有指定页面/控制器,它会定向到 ~/Default.aspx,如果指定了控制器,它会定向到该控制器?

【问题讨论】:

  • 您可以添加一个简单的控制器,然后将其重定向到您的 aspx 页面,或者在您的 global.ascx 上使用此代码: void Application_Start(object sender, EventArgs e) { System.Web.Routing.RouteTable .Routes.MapPageRoute("Home","Home/Index","~/Default.aspx"); }

标签: asp.net asp.net-mvc asp.net-mvc-routing


【解决方案1】:

我尝试在您的场景中进行试验,其中我有一个现有的 Web 表单应用程序并向其中添加了 mvc。基本上我遇到了两种解决方案。

第一:你可以忽略在路由配置中添加routes.MapPageRoute;这就是我所做的

public static class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.EnableFriendlyUrls();

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

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { action = "Index", id = UrlParameter.Optional }
            );
        }
    }

请注意,我在默认值中排除了控制器的值。现在,即使存在匹配项,并且如果该名称不存在控制器,则请求将回退到常规 ASP.Net 管道以查找 aspx 页面。使用这种方法,您可以请求像 http://localhost:62871/http://localhost:62871/Home/Indexhttp://localhost:62871/Home/http://localhost:62871/About.aspx 这样的网址

第二::在这种方法中,您可以在 Areas 文件夹下为您的新 MVC 工作创建一个 area,然后将 App_Start 中的 RouteConfig 设置为默认值,如下所示。

public static class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.EnableFriendlyUrls();
        }
    }

然后在您的区域注册类中定义路由,如下例所示。

public class AdminAreaRegistration : AreaRegistration
    {
        public override string AreaName
        {
            get
            {
                return "Admin";
            }
        }

        public override void RegisterArea(AreaRegistrationContext context)
        {
            context.MapRoute(
                "Admin_default",
                "{controller}/{action}/{id}",
                new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        }
    }

希望对您有所帮助。谢谢。

【讨论】:

  • 非常感谢,第一种方法效果很好,涵盖了我现在需要的东西,但是如果我需要以这种方式拆分东西,我可能会在以后开始使用第二种方法.这是一个巨大的帮助!
猜你喜欢
  • 2012-04-27
  • 2019-02-05
  • 2013-04-29
  • 2011-04-06
  • 1970-01-01
  • 1970-01-01
  • 2014-03-07
  • 2015-10-13
  • 2013-09-07
相关资源
最近更新 更多