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

时间:2010-05-18 05:52:21

标签: visual-studio-2010 asp.net-mvc-2 cassini iis-7.5

我有一个混合的asp.net web forms / mvc应用程序,我最近使用mvc2转换为.net 4。我已经设置了在IIS 7.5上运行的应用程序(在Windows 7上),并且该网站的Web表单部分运行正常,但MVC部分却没有。每当我尝试访问需要通过路由引擎的页面时,我都会

HTTP错误404.0 - 未找到
您要查找的资源已被删除,名称已更改或暂时不可用 模块IIS Web核心
通知MapRequestHandler
Handler StaticFile
错误代码0x80070002

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

我错过了什么?

3 个答案:

答案 0 :(得分:1)

根据我对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);
        }
    }
}

答案 1 :(得分:0)

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

答案 2 :(得分:0)

我有一个WebForms,当我添加API文件时,我也需要更改我的global.asax文件。我所做的只是创建一个新的空白API项目,然后复制文件。 在global.asax文件中添加文本时,会告诉您需要的其他库(例如WebApiConfig)。只需按照路径。