MVC 5 - Url.Content("~/") throws "Cannot use a leading .. to exit above the top directory" exception

StackOverflow https://stackoverflow.com/questions/22549551

Domanda

I have an interesting issue with the MVC UrlHelper.Content function when it's called from a page using the root of the url:

Issue occurs when called from mysite.com/some-content but does not occur when called from content 2 slashes deep such as mysite.com/content/some-content

When the following line of code is called with any ~ prefixed url:

@Url.Content("~/")

The following exception is thrown:

'Url.Content("~/")' threw an exception of type 'System.Web.HttpException' base {System.Runtime.InteropServices.ExternalException}: {"Cannot use a leading .. to exit above the top directory."} WebEventCode: 0

I am using Piranha CMS, and it is responsible for these root url site pages. However, looking at the type of object for the UrlHelper, it does not appear to be getting replaced by the CMS:

{Name = "UrlHelper" FullName = "System.Web.Mvc.UrlHelper"} [System.RuntimeType]: {Name = "UrlHelper" FullName = "System.Web.Mvc.UrlHelper"} base {System.Reflection.MemberInfo}: {Name = "UrlHelper" FullName = "System.Web.Mvc.UrlHelper"} Assembly: {System.Web.Mvc, Version=5.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35} AssemblyQualifiedName: "System.Web.Mvc.UrlHelper, System.Web.Mvc, Version=5.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" ...}

Any ideas what might be causing this issue?

From the web.config in case it highlights any version issues:

  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-1.3.0.0" newVersion="1.3.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="EntityFramework" publicKeyToken="b77a5c561934e089" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-5.1.0.0" newVersion="5.1.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
È stato utile?

Soluzione

This issue turned out to be a development environment only issue for me...

Running the site in Visual Studio Development Server from VS2012 results in this error, but once the web application was running under IIS the issue disappeared.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top