c# - ASP.NET MVC ActionLink vs RedirectToAction for images in areas -
i have issue image little hard explain make sense. using asp.net mvc 4 razor. have 1 area setup , using layout page in root. working fine except 1 small issue. when use actionlink in area, page renders fine including layout page except image on page. if use redirecttoaction in controller, renders fine including image. can see location of image rendered differently i'm not sure why or how fix it.
here's layout page located in root:
<!doctype html> <html lang="en"> <head> <meta charset="utf-8" /> <title>@viewdata("title") - elad</title> <link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" /> <meta name="viewport" content="width=device-width" /> @styles.render("~/content/css") @scripts.render("~/bundles/modernizr") </head> <body> <div id="container"> <header> <div class="content-wrapper"> <div class="float-left"> <p class="site-title"><img src="../../images/digital_blue_std.jpg" alt="" height="50px" style="vertical-align:middle" /> @html.actionlink("company name", "index", "home")</p> </div> <div class="float-right"> <section id="login"> @html.partial("_loginpartial") </section> <nav> <ul id="menu"> <li>@html.actionlink("home", "index", "home", new {.area = ""}, nothing)</li> <li>@html.actionlink("about", "about", "home", new {.area = ""}, nothing)</li> <li>@html.actionlink("contact", "contact", "home", new {.area = ""}, nothing)</li> </ul> </nav> </div> </div> </header> <div id="body"> @rendersection("featured", required:=false) <section class="content-wrapper main-content clear-fix"> @renderbody() </section> </div> <footer> <div class="content-wrapper"> <div class="float-left"> <p>© @datetime.now.year</p> </div> </div> </footer> </div> @scripts.render("~/bundles/jquery") @rendersection("scripts", required:=false) </body> </html>
and have area called ownedquote of views , controllers in. let's have url /ownedquote/aircraftregnumber/create got using redirecttoaction in controller (omitting http localhost portion of url due stack overflow requirements). if @ properties of image in case url image is: /images/digital_blue_std.jpg want. however, when use actionlink page page in same area, url same image is: /ownedquote/images/digital_blue_std.jpg. notice addition of area problem because image not exist in location. action link command just:
@html.actionlink("referral uw", "refertouw", "referral", new {.id = model.aircraft.id}, nothing)
everything else layout page , body section correct. i'm not sure why image url different when using html.actionlink vs redirecttoaction. need act same redirecttoaction.
first, don't use paths in application. want use known reference point. since using mvc 4, uses razor 2, razor natively understands site root. need this:
<img src="~/images/myimage.jpg">
where ~ root of site. in mvc3 had use html.content this:
<img src="@html.content("~/images/myimage.jpg")" >
the reason problem you're ending @ different url's, , because of mvc routing default action processing, can view same action 2 distinct url paths. problem images don't exist @ same offset both paths, problem. using site relative path solves problem.
remember when "../../blah" takes relative path url path, not pages actions path. because browser loading image, not server, , browser doesn't know other it's current path is.
Comments
Post a Comment