asp.net mvc - Add HTML content to a view Dynamically in MVC (without AJAX) -
asp.net mvc - Add HTML content to a view Dynamically in MVC (without AJAX) -
i have many static html files (lets 1.html 100.html). there way can create link files/get/1 (where "files" controller , "get" action). read file based on passed id , set file's content within site layout , send user.
in way format of html file preserved, , wouldn't need create view each file.
i new mvc , appreciate suggestion/hint. allow me know if question not clear.
thanks help in advance. reza,
edit: added ended doing.
answer: used darin said below, combined little bit of jquery , got needed. loading static html files within layout. here sample of did:
first created 2 methods in controller:
public actionresult gethelp(string id) { var folder = server.mappath(config.get().help_folder); var file = path.combine(folder, id + ".html"); if (!system.io.file.exists(file)) { homecoming httpnotfound(); } homecoming content(system.io.file.readalltext(file), "text/html"); } public actionresult gethelper(string id) { viewbag.helppath = id; homecoming view(); }
then created view called gethelper, uses layout, , added below code it:
<script type="text/javascript"> $(function () { var path = "@viewbag.helppath" path = "@url.content("~/helps/gethelp/")" + path; $('#help-content').load(path); }); </script> <div id="help-content"> </div>
and works perfectly. downside each page 2 server requests :(
something among lines:
public class filecontroller : controller { public actionresult index(string id) { var folder = server.mappath("~/somepathforthefiles"); var file = path.combine(folder, id + ".html"); if (!system.io.file.exists(file)) { homecoming httpnotfound(); } homecoming content(system.io.file.readalltext(file), "text/html"); } }
and if wanted user download files:
return file(file, "text/html", path.getfilename(file));
and because static files cache them decorating controller action [outputcache]
attribute:
[outputcache(location = outputcachelocation.downstream, duration = 10000, varybyparam = "id")] public actionresult index(string id)
asp.net-mvc
Comments
Post a Comment