简体   繁体   中英

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

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>

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.

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