asp.net mvc - Convert Server Side Include to Razor -


i'm in process of upgrading/modifying asp.net webforms application mvc5 - ideally, razor engine (to allow automatic mobile layout page switching, , take advantage of mvc features).

we have hundreds of server-side includes, named *.inc. absolute gobs of them. application large have convert pages piecemeal - there no way @ once. (for idea of how big project , how long it's been around, entire source control history in vss upon our migration tfs 10gb. includes lots of media files, still. it's big, , it's not going away.)

is there way can convert these #!include calls renderpartial() or renderaction() call without renaming every single file .cshtml?

duplicating include file poor recommendation, because breaks dry.

ideally, i'd one partial view handles - guess take filename argument, , that's one-liner, can train convert these trivially.

i want sort of file-level caching. loading output disk & dumping output stream nice small sites...but isn't small site.

we have ascx file caching many of these includes - have no clue how write corresponding 1 in razor.

edit 1: these part non-code includes, i'm sure user control definition has snuck in few. i'd have rewrite those, assume answer "these pure html, no code"

edit 2: have situations include files nested (it wasn't me swear). include file can include other files. have caching ascx control handles recursively, it's matter of porting code.

edit 3: potential solution must work in layout cshtml files. we've got includes in existing master pages.

off top of head can think of 4 ways tackle this.

note: didn't bother caching mechanisms because that's design decision outside of question.


create controller/action render include you:

public class includecontroller : controller {     // get: render     public actionresult render(string path)     {         return new includeresult(path);     } }  public class includeresult : actionresult {     private string _path { get; set; }      public includeresult(string path)     {         _path = path;     }     public override void executeresult(controllercontext context)     {         // possibly check cache here, or let mvc handle cache @ action level         // contents of include file output response         context.httpcontext.response.write(file.openread(_path));     } } 

in view

@html.action("render", "include", new {path = "~/includes/test.inc"}) 

note: tried using filepathresult , seems return odd characters, hence custom includeresult


create htmlhelper extension:

public static class htmlextension {     public static mvchtmlstring renderinclude(this htmlhelper helper, string path)     {         // check cache         string output = file.readalltext(path);         return new mvchtmlstring(output);     } } 

in view

@html.renderinclude("~/includes/test.inc") 

this option think fit trying best purest approach, might not fit 'the way mvc things'


extend razorviewengine include files well:

i didn't explore 1 far, , way overkill trying achieve following blog has code samples should you: http://weblogs.asp.net/imranbaloch/view-engine-with-dynamic-view-location

then use standard html.partial code in views

@html.patial("~/includes/test.inc"); 

expose include contents in property of view model:

this seems cumbersome, still it's option.

public class viewmodel {     public mvchtmlstring includecontents { get; set; }     ....      private getincludecontent(string path)     {         includecontents = new mvchtmlstring(file.readalltext(path));     } } 

then in view

@model.includecontents 

Popular posts from this blog