【问题标题】:Routing for action on default controller in ASP.NET MVCASP.NET MVC 中默认控制器上的路由操作
【发布时间】:2012-08-14 11:47:40
【问题描述】:

我有一个默认路由,所以如果我去 www.domain.com/app/ 它是例如 HomeController。我对控件有另一个动作,例如helloworld 但如果我去 www.domain.com/app/helloworld 它会失败并显示 404(毫无疑问期待 helloworld 控制器)。

如何在我的默认控制器上执行非默认操作,或者如何将 url /app/helloworld 映射到 helloworld 操作。我的路由如下所示:

routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);

routes.MapRoute( //this fails with same 404 like it does when it's ommitted
"Hello", // Route name
"app/helloworld", // URL with parameters
new { controller = "Home", action = "HellowWorld", id = UrlParameter.Optional } // Parameter defaults
);

基本上我需要:

/app/ => 控制器 = 主页,操作 = 索引

/app/helloworld => 控制器 = 主页,动作 = HelloWorld, 不是控制器 = HelloWorld,动作 - 索引

/app/other => 控制器 = 其他,动作 = 索引

【问题讨论】:

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


    【解决方案1】:

    将全局文件中的 2 routes 替换为:

    routes.MapRoute(
        "Default", // Route name
        "App/{controller}/{action}/{id}", // URL with parameters
        new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        );
    

    然后验证您的HomeController 包含:

    public ActionResult Index()
    {
        return View();
    }
    
    public ActionResult HelloWorld()
    {
        return View();
    }
    

    这样做是yoursite.com/app/ANYCONTROLLER/ANYACTION 将路由到控制器和动作(只要它们存在),默认的new { controller = "Home", action = "Index" 意味着如果有人转到yoursite.com/app/,它将自动路由到yoursite.com/app/Home/Index .

    如果这不起作用,请尝试删除 app/,它看起来像:

    routes.MapRoute(
        "Default", // Route name
        "{controller}/{action}/{id}", // URL with parameters
        new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
    );
    

    您所拥有的看起来像是您正在尝试路由 yoursite.com/Home/Index 而第二条路由措辞错误,我不确定它会做什么,但它应该看起来像 "app/HelloWorld/{action}/{id}" 然后 @987654334 而不是 "app/helloworld @ 会工作。这样如果有人去yoursite.com/app,它会自动显示yoursite.com/app/Home/HelloWorld。希望这有助于澄清一些事情。

    这就是你想要的答案

    由于某些原因,您不想为 hello world 部分创建新控制器,Global 文件中的 RegisterRoutes 应如下所示:

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    
            routes.MapRoute(
                "Hello World", // Route name
                "App/HellowWorld/{id}", // URL with parameters
                new { controller = "Home", action = "HelloWorld", id = UrlParameter.Optional }
                );
    
            routes.MapRoute(
                "Default", // Route name
                "App/{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
                );
        }
    

    虽然我不建议这样做。

    【讨论】:

    • 抱歉,这些不起作用。我已经在上面阐明了我的路由需求。谢谢
    • @DaveO 查看我的更新,并尝试yoursite.com/app/home/helloworld
    • @DaveO 我认为它有效;我很想知道你为什么不想创建一个单独的HelloWorld Controller,还是你最终选择了这条路?
    • 我使用了你的最后一个答案。我不想要另一个控制器,因为这个特定的应用程序只有一个,它是对表单提交的简单响应。无论如何,它工作得很好,谢谢!
    【解决方案2】:

    您应该只需要一条路线。不要对动作进行硬编码,而是将其设为变量,但包含默认值:

    routes.MapRoute( //this fails with same 404 like it does when it's ommitted
        "Hello", // Route name
        "app/{action}", // URL with parameters
        new { controller = "Home", action = "HelloWorld", id = UrlParameter.Optional } 
    );
    

    这样,当你访问 www.domain.com/app/ 时,它会得到默认操作(HelloWorld)。但是您仍然可以将路由中的操作指定为 www.domain.com/app/someotheraction。

    另外,请记住选择了第一个匹配的路由...因此您应该考虑将默认路由移到末尾,或者完全不使用它。在示例 www.domain.com/app/helloworld 中,它匹配默认路由,其中​​ app 作为控制器,helloworld 作为操作(这当然是无效的,因此是 404)。

    【讨论】:

    • 谢谢。你的意思是我不需要两个 MapRoute 调用?我尝试了上面的方法,但它以白屏失败。将我的第二个电话更改为此仍然会给我一个 404。
    • @DaveO 对——我通常会将第一条路由移到列表的末尾,这样它就可以作为“后备”使用,而不会抢占您的核心路由。
    猜你喜欢
    • 2010-12-15
    • 1970-01-01
    • 2019-06-06
    • 2018-07-30
    • 1970-01-01
    • 2011-07-15
    • 2011-02-23
    • 1970-01-01
    • 2010-11-01
    相关资源
    最近更新 更多