【问题标题】:how to rewrite mvc routes to aspx如何将 mvc 路由重写为 aspx
【发布时间】:2013-10-22 17:36:55
【问题描述】:

我有家庭控制器和索引操作方法。以下网址有效

http://localhost/home/index

是否有可能让它像下面那样工作

http://localhost/index.aspx

我正在 Global.asax 中尝试下面的代码,但不起作用

routes.MapPageRoute("MyPage", "create.aspx", "~/home/create");

路由配置

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

    routes.MapPageRoute("MyPage", "create.aspx", "~/home/create");

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

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

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

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-3 asp.net-mvc-4 model-view-controller


    【解决方案1】:

    您应该使用 MapRoute() 而不是 MapPageRoute(),因为您仍然指的是 MVC 控制器/动作:

            routes.MapRoute(
                name: "Default2",
                url: "index.aspx",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
    

    PS:记得在其他可能最终干扰它的路线之前注册新路线。

    【讨论】:

      【解决方案2】:

      你不能像你正在做的那样为非静态内容MapPageRoute。如果您不想为您的路线隐藏Controller,Customized 已经这样做了。如果您混合使用 MVC + WebForms,您应该参考本指南来了解如何配置您的路线。

      How to: Define Routes for Web Forms Applications

      【讨论】:

        猜你喜欢
        • 2012-12-08
        • 2018-09-09
        • 2012-04-05
        • 1970-01-01
        • 1970-01-01
        • 2013-09-01
        • 2017-05-13
        • 2013-09-07
        • 1970-01-01
        相关资源
        最近更新 更多