ASP.NET MVC 路由根级视图

发布于 2024-07-25 06:02:20 字数 559 浏览 6 评论 0原文

我以为这会很容易,但我完全感到困惑。

我希望一个控制器的视图位于应用程序的根级别,而不是位于该控制器的子目录中,但我无法弄清楚。

我想要这两个网址:

/Info - 这应该是控制器“Home”上的操作“Info”

/Admin/ - 这应该是控制器“Admin”上的操作“Index”(默认)

到目前为止,无论我做什么我已经尝试过,第一条路线最终会捕获两条路线。 我似乎无法将两者分开。

该信息页面甚至不需要控制器,它是静态的,但我确实想使用母版页。 可能有一种更简单的方法可以实现这一点,但我也还没有弄清楚。

我能想到的所有方法都是创建一个 Info 控制器,并将 Views/Home/Info 移动到 Views/Info/Index ,但这有一定的味道。

我能够使用以下方法在 Rails 中执行此操作:

  map.connect ':controller/:action/:id'
  map.connect ':action', :controller => 'home'

I thought this would be fairly easy, but I'm totally baffled.

I want one controller's views to be at the root level of the application, rather than in a subdirectory for that controller, but I cannot figure it out.

I'd like to have these two urls:

/Info - This should action "Info" on controller "Home"

/Admin/ - This should be action "Index" (default) on controller "Admin"

So far no matter what I've tried, the first route will end up catching both. I can't seem to separate the two.

That Info page doesn't even need a controller, it' static, but I do want to use a master page. There may be a much easier way to pull this off, but I haven't figured that out either.

All I can think of that would work, would be to create an Info controller, and move Views/Home/Info to Views/Info/Index, but that has a certain smell to it.

I was able to do this in rails using:

  map.connect ':controller/:action/:id'
  map.connect ':action', :controller => 'home'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

思念满溢 2024-08-01 06:02:20

你只需要正确的路线。 对于您的情况:

routes.MapRoute(
                "Info",
                "Info",
                new { controller = "Home", action = "Info" }

routes.MapRoute(
                "Admin",
                "Admin",
                new { controller = "Admin", action = "Index" }

但我建议您这种方法。

如果您需要更改视图/部分视图的默认物理位置,
查看如何创建自定义视图引擎。

You just need proper routes. In your case:

routes.MapRoute(
                "Info",
                "Info",
                new { controller = "Home", action = "Info" }

routes.MapRoute(
                "Admin",
                "Admin",
                new { controller = "Admin", action = "Index" }

But i recommend you this approach.

If you need to change default physical location of views/partialviews,
check out how to create custom view engines.

泡沫很甜 2024-08-01 06:02:20

您可以使用路由属性。

在你的路由配置文件中你应该有。

        routes.MapMvcAttributeRoutes();
        AreaRegistration.RegisterAllAreas();
        //code below should already be in your route config by default
        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );

然后在每个操作之上你可以有一个路由属性。

 [Route("info")]

您甚至可以通过添加参数和/或子文件夹来更高级地使用这些属性。

 [Route("blog/posts/{postId}")]

您可以将上述属性放在任何操作上,它看起来就像来自博客控制器一样。 但是,您甚至不需要博客控制器。 此外,{} 表示参数,因此只需确保您的操作采用与大括号中的参数相同的参数即可。 在这种情况下,参数将是

string postId

You can use Route Attributes.

In your route config file you should have.

        routes.MapMvcAttributeRoutes();
        AreaRegistration.RegisterAllAreas();
        //code below should already be in your route config by default
        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );

Then above every action you can have a route attribute.

 [Route("info")]

You can even get more advanced with these attributes by adding parameters, and/or subfolders

 [Route("blog/posts/{postId}")]

You can put the above attribute on any action, and it will appear as if it's originating from the blog controller. However, you don't even need a blog controller. Also the {} signify the parameter, so just make sure your action is taking the same parameter as what's in the curly braces. In this case the parameter would be

string postId
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文