在 ASP.net MVC 中显示没有控制器或操作的文件夹中的视图

发布于 2024-12-19 13:03:07 字数 393 浏览 1 评论 0原文

如果没有控制器/操作匹配,我想在文件夹中显示视图。

例如www.site.com/Home/Index,如果我有正常的默认路由{controller}/{action}/{id},那么我需要一个带有方法Index的HomeController。 Views 文件夹中有一个名为 Home 的文件夹和文件 Index.cshtml

如果我尝试 www.site.com/About/Index 我需要创建 AboutController 和方法索引。 但我只有 About 文件夹和 Index.cshtml 文件。

我希望如果默认路由不匹配,但我在 Views 文件夹中有一个文件夹和一个文件与模式匹配: {controller} 是文件夹 {action} 是视图;然后显示该视图。

我怎样才能做到这一点?

I would like to display the view in the folder if no controller/action matches.

For example www.site.com/Home/Index, if I have the normal default route {controller}/{action}/{id} then I need a HomeController with method Index.
And there is a folder in Views folder called Home and the file Index.cshtml

If i try www.site.com/About/Index i need to create the AboutController and the method index.
But I have just the folder About and file Index.cshtml.

I would like that if the default route does not match but I have a Folder and a File in the Views folder that match the patern: {controller} is the folder {action} is the view; then that view is displayed.

How could I achive that?

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

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

发布评论

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

评论(4

挽袖吟 2024-12-26 13:03:07

对于缺少的操作,您可以覆盖 HandleUnknownAction

对于缺少的控制器,您可以实现自定义 DefaultControllerFactory 并使用类似的内容覆盖 GetControllerInstance这:

protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType) {

   if (controllerType == null)
      return new DumbController();

   return base.GetControllerInstance(requestContext, controllerType);
}

class DumbController : Controller {

   protected override void HandleUnknownAction(string actionName) {

      try {
         View(actionName).ExecuteResult(this.ControllerContext);
      } catch (Exception ex) {
         throw new HttpException(404, "Not Found", ex);
      }
   }
}

For missing actions you can override HandleUnknownAction.

For missing controllers you can implement a custom DefaultControllerFactory and override GetControllerInstance with something like this:

protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType) {

   if (controllerType == null)
      return new DumbController();

   return base.GetControllerInstance(requestContext, controllerType);
}

class DumbController : Controller {

   protected override void HandleUnknownAction(string actionName) {

      try {
         View(actionName).ExecuteResult(this.ControllerContext);
      } catch (Exception ex) {
         throw new HttpException(404, "Not Found", ex);
      }
   }
}
掐死时间 2024-12-26 13:03:07

我最近在开发 AJAX Web 应用程序时遇到了同样的问题,其中大多数页面实际上不需要控制器(所有数据都通过 Web API 调用返回)。

让数十个控制器都通过单个操作返回视图似乎效率低下,因此我开发了 ControllerLess 插件,其中包含一个具有单个操作的默认视图控制器。

如果您为视图创建一个控制器,那么 MVC 将使用它。但是,如果您创建没有控制器的视图,则请求将通过默认插件控制器重新路由。

它适用于 C# 和 VB.NET,我们可以在 https://www.nuget.org/packages/ControllerLess 获取

源代码也可在 GitHub 上获取:https://github.com/brentj73/ControllerLess

I came across the same issue recently while developing AJAX web applications where most of the pages don't actually need a controller (all the data is returned via Web API calls).

It seemed inefficient to have dozens of controllers all with a single action returning the view so I developed the ControllerLess plugin which includes a default view controller behind the scenes with a single action.

If you create a controller for your view, then MVC will use that. However, if you create a view without a controller, the request is re-routed via the default plugin controller.

It works with C# and VB.NET and us available at https://www.nuget.org/packages/ControllerLess

The source code is also available on GitHub at https://github.com/brentj73/ControllerLess

噩梦成真你也成魔 2024-12-26 13:03:07

你不能也不应该按照你想要的方式去做。视图无法通过设计来寻址(在 /views 文件夹中的 web.config 中,有一个映射到 * 的 HttpNotFoundHandler 以确保这一点)

话虽如此,您在这里想要的并不是真正的标准,所以您为什么要这样做,也许我们能否根据背后的原因提出更好的建议?

You cannot and shouldn't the way you want. Views cannot be addressed by design (in the web.config in the /views folder theres an HttpNotFoundHandler mapped to * to ensure this)

With that said, what you want here is not really standard so why do you want to do this, maybe we can come up with a better suggestion based on the reason behind this?

金兰素衣 2024-12-26 13:03:07

从来没有尝试过这个,但这里有一个想法。您可以为路线设置约束,因此您应该能够创建一个与“{folder}/{file}”匹配的路线,您可以将它们限制为有效值(您可以在谷歌上搜索这个,或者在SO上搜索她),然后设置它以一些默认操作在 FileController(任意名称)上运行。然后,在该操作中,只需返回所需的视图即可。像这样的东西:

public class FileController : Controller {
    public ActionResult Default(string folder, string file) {
        return View(folder + "/" + file);
    }
}

Never tried this, but here's a thought. You can setup constraints for the routes, and thus you should be able to create a route matching "{folder}/{file}" where you constraint them to valid values (you can google this, or seach her on SO), and set it to run on a FileController (arbitrary name) with some default action. Then, in that action, simply return the desired view. Something like:

public class FileController : Controller {
    public ActionResult Default(string folder, string file) {
        return View(folder + "/" + file);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文