views:

476

answers:

2

i am porting over a website from asp.

i have one page that i can't figure out how to migrate it.

The page is dynamic in the sense that it reads in other html pages and sticks the content into the main "container" page. In the middle of the asp page it has sections like below

<%
Dim fso1, f11, ts1, s1  
Const ForReading1 = 1  
Set fso1 = CreateObject("Scripting.FileSystemObject")   
Set ts1 = fso1.OpenTextFile("" & Server.MapPath("newsletters/welcome.html") & "",     ForReading)  
s1 = ts1.ReadAll  
Response.Write s1  
ts1.Close  
set fso1 = nothing  
set f11 = nothing  
set ts1 = nothing  
set s1 = nothing  
%>

Any suggestions in ASP.net MVC for best way to read in other html pages and stick them into a page view.

+4  A: 

I assume that these are HTML fragments, not full pages. You could convert them to partial views -- pretty trivial, you just add the correct page directive and rename to .ascx. Then you would use Html.RenderPartial to include the partial in your main view. Another way would be to create your own HtmlHelper extension that works like RenderPartial but simply reads the named file and writes it to the response just like you are currently doing.

Ex1:

 <% Html.RenderPartial( "welcome.ascx" ); %>

Ex2:

 <% Html.RenderHtml(  Server.MapPath( "newletters/welcome.html" ) ); %>

Note that in the first case the view file needs to live in the Views directory. In the second case, you can reference the file from anywhere that the worker process has read access. You'll need to create the second method yourself. Perhaps something similar to:

 public static class MyHtmlHelperExtensions
 {
      public static void RenderHtml( this HtmlHelper helper, string path )
      {
           var reader = new StreamReader( path );
           var contents = reader.ReadToEnd();
           helper.ViewContext.HttpContext.Response.Write( contents );
      }
  }

Please note that you'll have to add error handling.

tvanfosson
in terms of the second case, i can't seem to find Html.RenderHtml method . . . can you advise
ooo
Sorry -- that's the one that you'd have to write. Create a static class with a public static method like: public static void RenderHtml( this HtmlHelper helper, string path ), then use it to read the file and write it to the response. Will update to clarify.
tvanfosson
ooo
The best way is to convert your html to a partial view and include it as in the first example. If you don't want to do the conversion you'll have to write some code. I've added a C# example of one possible solution.
tvanfosson
i tried using your example but i get this error:CS1061: 'System.Web.Mvc.HtmlHelper' does not contain a definition for 'RenderHtml' and no extension method 'RenderHtml' accepting a first argument of type 'System.Web.Mvc.HtmlHelper' could be found (are you missing a using directive or an assembly reference?)it compiles fine but blows up at runtime. is there a specific directory or namespace that this needs to be in.
ooo
You need to import the namespace into the view. <%@ Import Namespace="YourNameSpace" %> or added to the namespaces in the web.config.
tvanfosson
A: 

To read the contents of a text file in .Net, use the File.ReadAllText method.

The exact equivalent of your code snippet would be

<%= File.ReadAllText(Server.MapPath("newsletters/welcome.html")) %>
SLaks