ASP.NET MVC 4中的路由

时间:2022-08-26 20:49:07

I have an ASP.NET MVC 4 app. I'm trying to setup my routes in the RouteConfig.cs file. Essentially, I have two URLs I'm currently interested in. Those URLs are:

我有一个ASP.NET MVC 4应用程序。我正在尝试在RouteConfig.cs文件中设置我的路由。基本上,我有两个我目前感兴趣的URL。这些URL是:

/App
/App/Auth

My file structure looks like the following:

我的文件结构如下所示:

/Views
  App
    Index.cshtml
    Auth
      Index.cshtml
  Docs
    Index.cshtml
  Index.cshtml

In my RouteConfig.cs file, I have the following:

在我的RouteConfig.cs文件中,我有以下内容:

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

routes.MapRoute(
  "Index",
  "Index",
  new { controller = "Views", action = "Index" }
);

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

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

Oddly, when I visit /App/Auth it takes me to /App. For the life of me, I can't figure out why. Does anyone know how to do this? Thanks!

奇怪的是,当我访问/ App / Auth时,它将我带到/ App。对于我的生活,我无法弄清楚为什么。有谁知道如何做到这一点?谢谢!

2 个解决方案

#1


0  

The app is doing exactly what you asked. Your first route goes to controller "App" and action "Index". Move the folder Auth to same level as App and then change your route to:

该应用程序正在完成您的要求。您的第一条路线转到控制器“App”和动作“Index”。将文件夹Auth移动到与App相同的级别,然后将您的路线更改为:

routes.MapRoute(
  "Dashboard", // Route name
  "app/auth/{action}/{id}", // URL with parameters
  new { controller = "Auth", action = "Index", id = UrlParameter.Optional } 
);

An even better solution, forget the Auth controller and just create a view auth.cshtml in the App folder and an action "Auth" in the App controller, and then you can delete the route altogether and /app/auth will get handled by the default route.

一个更好的解决方案,忘记Auth控制器,只需在App文件夹中创建一个视图auth.cshtml,在App控制器中创建一个动作“Auth”,然后你可以完全删除路由,/ app / auth将由默认路线。

#2


0  

Change the route to this:

将路线更改为:

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

#1


0  

The app is doing exactly what you asked. Your first route goes to controller "App" and action "Index". Move the folder Auth to same level as App and then change your route to:

该应用程序正在完成您的要求。您的第一条路线转到控制器“App”和动作“Index”。将文件夹Auth移动到与App相同的级别,然后将您的路线更改为:

routes.MapRoute(
  "Dashboard", // Route name
  "app/auth/{action}/{id}", // URL with parameters
  new { controller = "Auth", action = "Index", id = UrlParameter.Optional } 
);

An even better solution, forget the Auth controller and just create a view auth.cshtml in the App folder and an action "Auth" in the App controller, and then you can delete the route altogether and /app/auth will get handled by the default route.

一个更好的解决方案,忘记Auth控制器,只需在App文件夹中创建一个视图auth.cshtml,在App控制器中创建一个动作“Auth”,然后你可以完全删除路由,/ app / auth将由默认路线。

#2


0  

Change the route to this:

将路线更改为:

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