使用子文件夹的Asp.net MVC路由 [英] Asp.net MVC routing using subfolders

查看:114
本文介绍了使用子文件夹的Asp.net MVC路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Controllers文件夹中的子文件夹:

I m trying to use subfolders within the Controllers folder:

结构如下:

  • 控制器(文件夹)
    • LoginController.cs
    • WelcomeController.cs
    • 设置(文件夹)
      • UsersController.cs
      • Controllers (Folder)
        • LoginController.cs
        • WelcomeController.cs
        • Settings (Folder)
          • UsersController.cs

          我有几个问题. 当我从LoginController执行返回RedirectToAction("Index","welcome")时,URL看起来像 http://mywebsite.local/settings/welcome 我以为我会收到404错误.

          I've several problems. When I perform a return RedirectToAction("Index", "welcome") from my LoginController, the url looks like http://mywebsite.local/settings/welcome I thought I will get a 404 error..

          如何进行重定向启动 http://mywebsite.local/welcome 并在我收到404错误启动 http://mywebsite.local/settings/welcome

          How to make the redirection launches http://mywebsite.local/welcome and get a 404 error when I launch http://mywebsite.local/settings/welcome

          我真的需要使用Areas吗?

          Do I really need to use Areas?

          这是我的RouteConfig.cs

          This is my RouteConfig.cs

                  routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
                  routes.MapRoute(
                      name: "settings",
                      url: "settings/{controller}/{action}/{id}",
                      defaults: new { controller = "Users", action = "Index", id = UrlParameter.Optional }
                  );            
          
                  routes.MapRoute(
                      name: "Default",
                      url: "{controller}/{action}/{id}",
                      defaults: new { controller = "Login", action = "Index", id = UrlParameter.Optional }
                  );            
          

          推荐答案

          此处的控制器的文件夹结构与您看到的内容几乎没有关联.如果路线可行,您可以维护该结构并实现目标.

          The folder structure of your controllers here has very little relevance to what you are seeing. You could maintain that structure and accomplish your goal if the route would work.

          但是,您已经指定了两个匹配的路由,可以用来对同一动作进行编码,并且它只是赋予第一个优先级.看到,路由不仅向前工作,MVC还反向使用它们来处理您的____Action()语句.您可以改为使用RedirectToRouteRedirectToAction("Index", "welcome")中指定一个命名的路由(例如设置"),或在路由表中手动指定所有带有前缀的路由.但是为什么要从这样有意冲突的路线开始您的项目呢?

          However, you've specified two matching routes that can be used to encode the same action, and it is simply giving the first one priority. See, routes don't just work forwards, MVC uses them in reverse, too, to process your ____Action() statements. You could specify a named route (e.g. "settings") in your RedirectToAction("Index", "welcome"), by using RedirectToRoute instead, or manually specify all the routes that have prefixes in your route table. but why start your project off with intentionally conflicting routes like this?

          正如Joe R所说,Areas将使您的生活更轻松.为什么?主要是因为有一个附加的内置route参数可以完全执行您想要的操作.真的,问题变成了为什么要避开Areas?"

          As Joe R said, Areas will make your life easier. Why? Mainly because there is an additional built-in route parameter to do exactly what you want. Really, the question becomes "why avoid Areas?"

          这篇关于使用子文件夹的Asp.net MVC路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆