这是正确的 MVC 路由设置还是还有其他方法?

发布于 2024-12-27 05:52:17 字数 672 浏览 1 评论 0原文

我想保留我的网站的根目录用于标准 Web 表单,并将 MVC 页面放在子目录 Views 中,因此我有以下内容。

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

但是,尽管这可行并且我看到了一些解决方法,但我不太清楚很高兴 RedirectToAction 似乎将我引导到错误的页面,例如

return RedirectToAction("Index", "Home");

带我到 http://localhost/Views ,这给了我一个未找到的资源,并且 HomeController 上的 Index 操作不会触发。有没有更好的方法来实现我想要的东西,或者我错过了一些明显的东西?

I want to reserve the root of my website to be for standard webforms and have the MVC pages in a subdirectory Views so I have the following..

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

However, even though this works and I see a few work-arounds, I'm not quite happy that the RedirectToAction seems to direct me to the wrong page e.g.

return RedirectToAction("Index", "Home");

Takes me to http://localhost/Views which gives me a resource not found and the Index action on the HomeController doesn't fire. Is there a better way of implementing what I want here or am I missing something obvious?

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

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

发布评论

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

评论(1

穿越时光隧道 2025-01-03 05:52:17

如您所知,Views 是 ASP.NET MVC 中的保留名称。这是一个现有的目录。您可以在路由定义中将 RouteExistingFiles 设置为 true

public static void RegisterRoutes(RouteCollection routes)
{
    routes.RouteExistingFiles = true;

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

    routes.MapRoute(
        "Default",
        "views/{controller}/{action}/{id}",
        new { controller = "home", action = "index", id = UrlParameter.Optional }
    );

}

现在,当您导航到 http://example.com/views>http://example.com/views/homehttp://example.com/views/home/index 它将是 Index 操作将被执行的 Home 控制器。

As you know Views is kind of a reserved name in ASP.NET MVC. It's an existing directory. You could set the RouteExistingFiles to true in your route definitions:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.RouteExistingFiles = true;

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

    routes.MapRoute(
        "Default",
        "views/{controller}/{action}/{id}",
        new { controller = "home", action = "index", id = UrlParameter.Optional }
    );

}

Now when you navigate to http://example.com/views or http://example.com/views/homeor http://example.com/views/home/index it will be the Index action of Home controller that will get executed.

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