MVC 路由误区

发布于 2025-01-06 22:25:10 字数 858 浏览 0 评论 0原文

我正在开发 ASP.NET MVC 应用程序。由于某种原因,每次我认为我了解路由时,就会弹出一些我不理解的内容。目前,我有两条路线我似乎无法弄清楚。我的目录结构如下所示

- Views
  - Internal
    - Profile
      - Index.cshtml
    - Input
      - Page1.cshtml

在我的 global.asax.cs 文件中,我添加了以下映射:

  routes.MapRoute(
    "UserProfileInfo",
    "{controller}/profile",
    new { controller = "Internal", action = "UserProfileInfo" }
  );


  routes.MapRoute(
    "Page1",
    "{controller}/input/page1",
    new { controller = "Internal", action = "Page1" }
  );

在 MyController 中,我有以下内容:

  public ActionResult UserProfileInfo()
  {
    return View("~/Views/internal/profile/Index.cshtml");
  }

  public ActionResult Page1()
  {
    return View("~/Views/internal/input/Page1.cshtml");
  }

我想将我的操作存储在单个控制器中。我以为我已经正确设置了一切。但我仍然收到 404 错误。我做错了什么?

I am working on an ASP.NET MVC application. For some reason, everytime I think I understand routing, something pops up that I don't understand. Currently, I have two routes that I can't seem to figure out. My directory structure looks like the following

- Views
  - Internal
    - Profile
      - Index.cshtml
    - Input
      - Page1.cshtml

In my global.asax.cs file, I have added the following mappings:

  routes.MapRoute(
    "UserProfileInfo",
    "{controller}/profile",
    new { controller = "Internal", action = "UserProfileInfo" }
  );


  routes.MapRoute(
    "Page1",
    "{controller}/input/page1",
    new { controller = "Internal", action = "Page1" }
  );

In MyController, I have the following:

  public ActionResult UserProfileInfo()
  {
    return View("~/Views/internal/profile/Index.cshtml");
  }

  public ActionResult Page1()
  {
    return View("~/Views/internal/input/Page1.cshtml");
  }

I want to store my actions in a single controller. I thought I had everything setup properly. But I continue to get a 404. What am I doing wrong?

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

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

发布评论

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

评论(1

走过海棠暮 2025-01-13 22:25:10

在调用 MapRoute 时从控制器名称中删除“Controller”后缀,以创建到名为 InternalController 的类的映射。当寻找匹配的实现时,控制器后缀由框架附加。例如:

    routes.MapRoute( 
    "UserProfileInfo", 
    "{controller}/profile", 
    new { controller = "Internal", action = "UserProfileInfo" } 
); 

Remove the "Controller" suffix from the controller name in your calls to MapRoute to create a mapping to a class called InternalController. The Controller suffix is appended by the framework when looking for a matching implementation. e.g.:

    routes.MapRoute( 
    "UserProfileInfo", 
    "{controller}/profile", 
    new { controller = "Internal", action = "UserProfileInfo" } 
); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文