开发者

Serving static content to my action using MVC's convention approach

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 开发者_JAVA技巧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?


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")) %>


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>
  }


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜