简体   繁体   中英

Path to image under MVC5 Area

I am pretty new to MVC5 and I am trying to have a Content folder locally under one of my Areas. Unfortunately I cannot seem to get the image to render in the output since the path becomes incorrect(?) and I can´t figure out how to correctly reference it.

In my solution I the following:

ProjectX
  Areas
     Area1
        Controller
           Area1DefaultController
        Content
           Images
               image1.png
        Views
               View1.cshtml

In my View i use this:

@Url.Content("~/Areas/Area1/Content/Images/image1.png")

I have also tried..

@Url.Content("~/Content/Images/image1.png")
@Url.Content("/Content/Images/image1.png")
@Url.Content("Content/Images/image1.png")

..and the result in all cases are broken images (ie the path is incorrect). I guess the routing somehow prevents the image from being retrieved but I can´t figure out how to fix the problem.

This is my area registration:

public class Area1Registration : AreaRegistration
{
    public override string AreaName
    {
        get { return "Area1"; }
    }

    public override void RegisterArea(AreaRegistrationContext context)
    {
        context.MapRoute(
            "Area_default",
            "Area/{action}/{id}",
            new { controller = "Area1Default", action = "Index", id = UrlParameter.Optional }
        );
    }
}

UPDATE : ReSharper is able to resolve the path in the markup.

How can I find my resources under Content?

The reason I received a 404 was the BlockViewHandler in the Area1 web.config . I created a new web.config and placed in my Static folder to solve the problem.

<?xml version="1.0" encoding="utf-8"?>
<configuration>  
  <system.webServer>
    <validation validateIntegratedModeConfiguration="false" />
    <handlers>
      <remove name="BlockViewHandler" />     
    </handlers>
  </system.webServer>
</configuration>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM