代码之家  ›  专栏  ›  技术社区  ›  Guy

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

  •  1
  • Guy  · 技术社区  · 16 年前

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

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

    我正在通过VS2010调试此网站(因此我将其设置为使用IIS而不是Cassini),当我在应用程序启动函数中放置一个断点时,它不会被击中,因此路由不会注册。当我在页面加载函数的某个ASPX页面代码中放置一个断点时,它会被击中。因此,问题似乎是该路由没有被注册。

    我错过了什么?

    3 回复  |  直到 8 年前
        1
  •  1
  •   Maxim Zaslavsky Macy Abbey    15 年前

    根据我在ASP.NET MVC方面的经验, 我看到了 Default.aspx 要使IIS正常工作,需要页 . 我正在使用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. --%>
    

    默认.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);
            }
        }
    }
    
        2
  •  0
  •   Shay Friedman    16 年前

    我记得在某个地方读到过一个类似的问题,global.asax事件没有被触发。尝试删除global.asax文件并再次添加(只需记住重新添加所需的路由代码)。

        3
  •  0
  •   Rodney Ellis    8 年前

    我有一个webforms,当我添加API文件时,我也需要更改global.asax文件。我所做的只是创建一个新的空白API项目,然后复制文件。 当您在global.asax文件中添加文本时,会被告知您需要的其他库(如webapiconfig)。沿着小路走。