我希望此网址(/views/shared/_layout.cshtml)生成404响应代码&显示我的自定义404页面。它当前生成此500错误和堆栈跟踪:
异常详细信息:System.Web.HttpException:无法提供带前导下划线(“_”)的文件。
堆栈跟踪:
[HttpException(0x80004005):无法提供带前导下划线(“_”)的文件。
System.Web.WebPages.WebPageRoute.GetRouteLevelMatch(String pathValue,IEnumerable 1 supportedExtensions, VirtualPathFactoryManager virtualPathFactoryManager) +291
System.Web.WebPages.WebPageRoute.MatchRequest(String pathValue, IEnumerable
1 supportedExtensions,VirtualPathFactoryManager virtualPathFactoryManager)+441
System.Web.WebPages.WebPageRoute.DoPostResolveRequestCache(HttpContextBase context)+222
System.Web.WebPages.WebPageHttpModule.OnApplicationPostResolveRequestCache(Object sender,EventArgs e)+146
System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()+220
System.Web.HttpApplication.ExecuteStep(IExecutionStep step,Boolean& completedSynchronously)+120
答案 0 :(得分:1)
确保Views\web.config
文件中包含以下设置:
<appSettings>
<add key="webpages:Enabled" value="false" />
</appSettings>
答案 1 :(得分:1)
如果你看一下抛出异常的代码:它发生了,因为WebPageHttpModule已经连接了一个试图解析路由的PostResolveRequestCache处理程序。然后它会检测到无效的路径和barfs。
也许如果你能够在WebPageHttpModule之前注册自己的IHttpModule,你可以在默认的PostResolveRequestCache处理程序之前对请求进行操作。
另一个选择可能是挂钩Application_Error事件,然后调查异常并在适当的时候返回404。
答案 2 :(得分:0)
如果您在集成模式下使用IIS,可能会在服务器级别阻止:
<system.webServer>
<!--- any others here --->
<handlers>
<!--- any others here --->
<add name="BlockViewHandler" path="*.cshtml" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
</handlers>
</system.webServer>
答案 3 :(得分:0)
这是一个老问题,但如果有人遇到同样的问题,你可以尝试这个解决方案。
问题出现是因为文件名以下划线开头(导致返回HTTP 500)。要解决此问题,您应该重命名该文件以删除下划线。现在,只要您有网页,就应该开始获取HTTP 404:在web.config中将Enabled设置为false。
<appSettings>
<add key="webpages:Enabled" value="false" />
</appSettings>
答案 4 :(得分:0)
我不知道,你还是得到了正确答案。
今天我才知道:ASP.NET网页有一个像 -
这样的属性防止浏览文件
使用ASP.NET,名称以下划线开头的文件不能 从网上浏览。
如果您想阻止内容块或布局文件 您的用户查看,将文件重命名为:
_header.cshtm
_footer.cshtml
_Layout.cshtml
查看此信息的链接是:http://www.w3schools.com/aspnet/webpages_layout.asp。
记住这一点,应该使用下划线(_)来启动任何文件名,以保护您的部分文件不被网页浏览。
我认为现在理由很清楚。
答案 5 :(得分:0)
答案 6 :(得分:0)
对我来说,这是一个缺少的Sitecore.Mvc.config导致Timm提出的问题