Web 服务器上不存在指定的目录或文件

发布于 2024-09-01 18:24:12 字数 473 浏览 4 评论 0原文

我有一个混合的 ASP.NET Web 表单/MVC 应用程序,最近我使用 MVC2 将其转换为 .NET 4。我已将该应用程序设置为在 IIS 7.5(在 Windows 7 上)上运行,并且该网站的 Web 表单部分运行正常,但 MVC 部分运行不正常。每当我尝试访问需要通过路由引擎的页面时,我都会收到

HTTP 错误 404.0 - 未找到
您要查找的资源已被删除、更名或暂时不可用。
模块 IIS Web 核心
通知MapRequestHandler
处理程序静态文件
错误代码 0x80070002

我正在通过 VS2010 调试此网站(因此我将其设置为使用 IIS 而不是 Cassini),当我在 Application_Start 函数中放置断点时,它永远不会被命中,因此路由永远不会注册。当我在 aspx 页面代码隐藏之一的 Page_Load 函数中放置一个断点时,它会被击中。所以看来问题是路线没有被注册。

我缺少什么?

I have a hybrid asp.net web forms / mvc application that I recently converted to .net 4 with mvc2. I have set-up that application to run on IIS 7.5 (on Windows 7) and the web forms part of the site is running okay but the MVC part is not. Whenever I try and access a page that needs to go through the routing engine I get

HTTP Error 404.0 - Not Found
The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.
Module IIS Web Core
Notification MapRequestHandler
Handler StaticFile
Error Code 0x80070002

I'm debugging this web site through VS2010 (so I've set-it-up to use IIS instead of Cassini) and when I put a break point in the Application_Start function it is never hit so the routes are never registered. When I put a break point in the Page_Load function in one of the aspx page code-behinds it gets hit. So it seems that the problem is that the route is not being registered.

What am I missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

苍暮颜 2024-09-08 18:24:12

根据我使用 ASP.NET MVC 的经验,我发现 IIS 需要 Default.aspx 页才能正常运行。我正在使用 ASP.NET MVC 1 模板中包含的页面。不幸的是,ASP.NET MVC 2 不包含此页面(据我所知),因此您应该将以下内容添加到您的项目中:

Default.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="YourNamespace._Default" %>

<%-- Please do not delete this file. It is used to ensure that ASP.NET MVC is activated by IIS when a user makes a "/" request to the server. --%>

Default.aspx.cs:

using System.Web;
using System.Web.Mvc;
using System.Web.UI;

namespace YourNamespace
{
    public partial class _Default : Page
    {
        public void Page_Load(object sender, System.EventArgs e)
        {
            // Change the current path so that the Routing handler can correctly interpret
            // the request, then restore the original path so that the OutputCache module
            // can correctly process the response (if caching is enabled).

            string originalPath = Request.Path;
            HttpContext.Current.RewritePath(Request.ApplicationPath, false);
            IHttpHandler httpHandler = new MvcHttpHandler();
            httpHandler.ProcessRequest(HttpContext.Current);
            HttpContext.Current.RewritePath(originalPath, false);
        }
    }
}

From my experience with ASP.NET MVC, I've seen that a Default.aspx page is required for IIS to function correctly. I'm using the page that was included in the ASP.NET MVC 1 template. Unfortunately, the ASP.NET MVC 2 does not include this page (to the best of my knowledge), so you should add the following to your project:

Default.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="YourNamespace._Default" %>

<%-- Please do not delete this file. It is used to ensure that ASP.NET MVC is activated by IIS when a user makes a "/" request to the server. --%>

Default.aspx.cs:

using System.Web;
using System.Web.Mvc;
using System.Web.UI;

namespace YourNamespace
{
    public partial class _Default : Page
    {
        public void Page_Load(object sender, System.EventArgs e)
        {
            // Change the current path so that the Routing handler can correctly interpret
            // the request, then restore the original path so that the OutputCache module
            // can correctly process the response (if caching is enabled).

            string originalPath = Request.Path;
            HttpContext.Current.RewritePath(Request.ApplicationPath, false);
            IHttpHandler httpHandler = new MvcHttpHandler();
            httpHandler.ProcessRequest(HttpContext.Current);
            HttpContext.Current.RewritePath(originalPath, false);
        }
    }
}
耳根太软 2024-09-08 18:24:12

我记得在某处读到过有关 Global.asax 事件未触发的类似问题。尝试删除 Global.asax 文件并再次添加(只需记住重新添加所需的路由代码)。

I remember reading somewhere about a similar problem that Global.asax events weren't firing. Try to remove the Global.asax file and add it again (just remember to re-add the needed routes code).

妞丶爷亲个 2024-09-08 18:24:12

我有一个 WebForms,当我添加 API 文件时,我也需要更改我的 global.asax 文件。我所做的就是创建一个新的空白 API 项目,然后复制文件。
当您在 global.asax 文件中添加文本时,您会收到所需的其他库(例如 WebApiConfig)。只需跟随踪迹即可。

I had a WebForms, and when I added the API files I needed to change my global.asax file too. All I did was create a new blank API project and then copy the files across.
When you add the text in the global.asax file, you get told of other libraries you need (such as WebApiConfig). Simply follow the trail.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文