使用 MVC 的约定方法为我的操作提供静态内容

发布于 2024-10-30 18:41:06 字数 396 浏览 0 评论 0原文

我正在考虑在我正在创建的大型 Web 应用程序上外包一些页面内帮助,并希望在我们准备好时能够轻松地将这些内容添加到我们的页面中。

所以我想我可以创建一个系统,在其中我可以将文本文件添加到操作期望其视图所在的同一文件夹中,并读出该文件的内容,该文件中的内容可以传递回视图以显示。要么创建一个可以做同样事情的助手。

示例

Controllers
   HomeController.cs

Views
   Home
      Index.aspx
      Index.txt

然后,Index.aspx 将可以访问 Index.txt 中的内容。

我将如何开始创建这个系统。 .NET MVC 中是否有我可以利用的内置类?

I'm looking at outsourcing some in-page help on a large web application I am creating and would like to make it really easy for this content to added to our pages when we're ready for it.

So I was thinking I could create a system where I can add text files to the same folder where an action expects it's view to be and read out the content of that file the content in that file can be passed back to the view to display. Either that or create a helper that would do the same.

Example

Controllers
   HomeController.cs

Views
   Home
      Index.aspx
      Index.txt

Index.aspx would then have access to the content in Index.txt.

How would I start going about creating this system. Are there any built in classes in .NET MVC that I could take advantage of?

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

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

发布评论

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

评论(3

迷途知返 2024-11-06 18:41:06

昨天提出了类似的问题: 包括静态 html文件从 ~/Content 到 ASP.NET MVC 视图

基本上,您可以使用 File.ReadAllText 从文件中读取文本并将其包含在视图中,这样您的 index.aspx 文件中就会有类似的内容

<%= File.ReadAllText(Server.MapPath("~/Views/Home/index.txt")) %>

A similar question was asked yesterday: Including static html file from ~/Content into ASP.NET MVC view.

Basically you can read the text from the file and include it inside your view by using File.ReadAllText so you would have something like this inside your index.aspx file

<%= File.ReadAllText(Server.MapPath("~/Views/Home/index.txt")) %>
妄断弥空 2024-11-06 18:41:06

我会在 Content 文件夹中创建一个并行层次结构,并将文件放在那里,可能是 HTML 形式。然后,您可以使用并行层次结构约定通过 AJAX 在视图中简单地加载它们。

Content
   Help
     Home
       index-help.html
       about-help.html
     Foo
       index-help.html
       bar-help.html

然后在您的视图中,

  <div class="help">
  <noscript>
      <a href='@Url.Content( "~/content/help/home/index-help.html" )'>Click for Help</a>
  </noscript>
  </div>

  <script type="text/javascript">
      $(function() {
          $('.help').load( '@Url.Content( "~/content/help/home/index-help.html" )' );
      });
  </script>

如果您的路线一致,您还可以从视图中的 RouteData 提取控制器/操作,并将其移动到您的 _Layout.cshtml 文件,并使用路线数据提供的路径。

 @{
    var controller = ViewContext.RouteData["controller"] as string;
    var action = ViewContext.RouteData["action"] as string;
    var url = Url.Content( string.Format( "~/content/help/{0}/{1}-help.html", controller, action ) );

  <div class="help">
  <noscript>
      <a href="@url>Click for Help</a>
  </noscript>
  </div>

 <script type="text/javascript">
      $(function() {
          $('.help').load( "@url" );
      });
  </script>
  }

I'd create a parallel hierarchy in the Content folder and put the files there, probably as HTML. Then you can simply load them via AJAX in the view using the parallel hierarchy convention.

Content
   Help
     Home
       index-help.html
       about-help.html
     Foo
       index-help.html
       bar-help.html

Then in your views

  <div class="help">
  <noscript>
      <a href='@Url.Content( "~/content/help/home/index-help.html" )'>Click for Help</a>
  </noscript>
  </div>

  <script type="text/javascript">
      $(function() {
          $('.help').load( '@Url.Content( "~/content/help/home/index-help.html" )' );
      });
  </script>

You may also be able to extract the controller/action from RouteData in the view if your routes are consistent and move this to your _Layout.cshtml file with the path being provided by route data.

 @{
    var controller = ViewContext.RouteData["controller"] as string;
    var action = ViewContext.RouteData["action"] as string;
    var url = Url.Content( string.Format( "~/content/help/{0}/{1}-help.html", controller, action ) );

  <div class="help">
  <noscript>
      <a href="@url>Click for Help</a>
  </noscript>
  </div>

 <script type="text/javascript">
      $(function() {
          $('.help').load( "@url" );
      });
  </script>
  }
零崎曲识 2024-11-06 18:41:06

一种可能的解决方案是将它们存储为 xml 文件,这些文件是从视图期望的模型序列化的。然后,您可以创建一个操作筛选器,用数据填充返回的模型来自 XML 文件。我希望这有帮助。

One possible solution would be to store them as xml file instead, that are serialized from the model the view is expecting. You could then create an Action Filter populate the model being returned with the data from the XML file. I hope that helps.

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