c# - Pass generated HTML to a view and replace links with Url.Action -


i have requirement display webpages stored in database part of mvc project. lets have index page (stored in db) link points page1.html (also stored in db) <a href="page1.html">. have controller (call webpagegenerator) takes page name argument, retrieves html database, manipulation , passes resulting string view. view nothing more

<div class="generatedpage"> @html.raw(viewbag.pagedata) </div> 

(yes i've realised shouldnt use viewbag etc etc i'll deal later unless of course somehow cause of problems...) if pass along html example index page, link page1 not work. if instead somehow pointed localhost:port/myproject/webpagegenerator/page1.html work.

i've spent last day struggling different variations of url.action pass view, nothing working.

my controller looks following

[httpget, actionname("index")] public actionresult webpagegenerator(string id) {     stringbuilder pagetext = gethtmlfromdb(id);     string linkname = getlink(pagetext) //should contain <a href="page1.html">     string newlink = "'@url.action(\"index\", \"webpagegenerator\", new {" + string.format("id={0}", linkname.onlypagename()) + "})'";     pagetext.replace(linkname, newlink);     viewbag.pagedata = pagetext.tostring();     return view(); } 

unfortunately in resulting page, link comes out localhost:port/myproject/@url.action(%22index%22,%20%22webpagegenerator%22,%20new%20%7bid=%22page1.html%22%7d) if hover on it, , inspecting shows <a href='@url.action("index", "webpagegenerator", new {id="page1.html"})'>

obviously, link doesnt work. 1 way have got working hardcode url in controller such:

var addurl = httpcontext.request.urlreferrer.absoluteuri; string newlink = addurl.contains("webpagegenerator") ? linkname.onlypagename()) : string.format("webpagegenerator/{0}", linkname.onlypagename()); 

i'm having check referring url otherwise if i'm viewing page1.html contains link page2.html, link point me localhost:port/myproject/webpagegenerator/webpagegenerator/page2.html

this seems dirty fix i'm not sure work when application published/deployed. appreciated!

ps - first post on so. on years.

you wrapping call @url.action(...) string. problem razor engine won't see directive , output it.

change call url.action(...) , should work fine.

string newlink = url.action("index", "webpagegenerator", new { id=linkname.onlypagename()}); 

and full snippet:

    [httpget, actionname("index")]     public actionresult webpagegenerator(string id)     {         stringbuilder pagetext = gethtmlfromdb(id);         string linkname = getlink(pagetext) //should contain <a href="page1.html">         string newlink = url.action("index", "webpagegenerator", new { id=linkname.onlypagename()});         pagetext.replace(linkname, newlink);         viewbag.pagedata = pagetext.tostring();         return view();     } 

msdn url.action: https://msdn.microsoft.com/en-us/library/system.web.mvc.urlhelper.action(v=vs.118).aspx


Comments

Popular posts from this blog

javascript - Clear button on addentry page doesn't work -

c# - Selenium Authentication Popup preventing driver close or quit -

tensorflow when input_data MNIST_data , zlib.error: Error -3 while decompressing: invalid block type -