by StoneJunction
10. January 2012 03:56
If you stop the debugger in a MVC controller and have a dig down in ViewEngines.Engines you will find a property PartialViewLocationFormats

When using @Html.Partial(“{_YourView}”) razors view engine looks in the 4 default locations. The {0} represents the view name and {1} is the controller name.
If you move your partials to a different directory you will get an error screen as it can’t find the file on the default paths its loading. To work around this you can set your own custom ViewEngine with the the paths to your specific location. So for example if you were storing the partials in /Partials then you would use:
public class MyViewEngine : RazorViewEngine
{
private static readonly string[] NewPartialViewFormats = new[]
{
"~/Views/Shared/Partials/{0}.cshtml",
};
public MyViewEngine()
{
base.PartialViewLocationFormats = base.PartialViewLocationFormats.Union(NewPartialViewFormats).ToArray();
}
}
And then in the global.asax.cs you would call it at startup:
protected void Application_Start(Object sender, EventArgs e)
{
ViewEngines.Engines.Add(new MyViewEngine());
}
by StoneJunction
6. January 2012 23:58
I am just getting up to speed on MVC3 with Razor and when creating a partial view ("_TestPartial.cshtml") and including it on another page through with:
@Html.RenderPartial("_TestPartial")
I got the error:
CS1502: The best overloaded method match for 'System.Web.WebPages.WebPageExecutingBase.Write(System.Web.WebPages.HelperResult)' has some invalid arguments
Looking at RenderPartial it returns a void. Internally it writes directly to the response. A quick razor syntax change to:
@{ Html.RenderPartial("_TestPartial"); }
sorted the problem. Now for the interesting problem of getting MVC partials to render on classic aspx webpages.
by StoneJunction
7. October 2011 20:17
I ran into a problem yesterday with the File.WriteAllText method when saving large text files to disk.
I had a piece of code similar to this:
public void Test(List<String> lines)
{
var output = new StringBuilder();
foreach (String line in lines)
{
output.Append(line).Append(Environment.NewLine);
}
File.WriteAllText(@"c:\test.txt", output.ToString());
}
which worked fine for small string lists. However when the list of strings ran to the 100's of Mb I got System Out of Memory errors. A simple google seemed to show a few people with similar problems but no real solutions. Pushed for time I copped out and replaced it with a good old fashioned StreamWriter:
public void Test(List<String> lines)
{
var file = new StreamWriter(@"c:\test.txt");
foreach (String line in lines)
{
file.WriteLine(line);
}
file.Close();
}