【问题标题】:MVC5 app not working in IIS 7 - Error 404 - File or directory not foundMVC5 应用程序无法在 IIS 7 中运行 - 错误 404 - 找不到文件或目录
【发布时间】:2015-05-27 18:36:46
【问题描述】:

那里!

经过一番周折,我终于在我的开发机器上运行了我的第一个 MVC 应用程序。

场景如下:

  • 它是一个 Intranet 应用程序(因此我使用的是 Windows 身份验证)
  • 这是一个 MVC 5 应用程序(带有 Bootstrap 和 jQuery 的东西 - 没有 .aspx 页面)
  • 实体框架 6(代码优先)
  • SQL Server 精简版
  • VS 2013 速成版
  • 服务器:Windows 2008 R2,运行 IIS 7
  • 已安装修补程序 KB980368
  • IIS 注册到 asp.net(命令 aspnet_regiis.exe -i ran)

我在服务器上有一些管理权限,所以我创建了一个文件夹来保存应用程序。然后,我使用VS2013,按照如下配置发布了应用:

  • 发布方法:文件系统
  • 目标位置:服务器上创建的文件夹
  • 配置:发布
  • 数据库:此发布方法不支持数据库发布

服务器上的目标被以下文件夹填充:

  • bin(System.Web.MVC、System.Web.Razor 等)
  • 内容
  • 字体
  • 脚本
  • 观看次数

以及以下文件:

  • Global.asax
  • packages.config
  • web.config

在 IIS 7 中,我添加了一个虚拟目录,其物理路径指向之前创建的该文件夹。之后,我将虚拟目录转换为 Application。

尝试运行应用程序并得到一个奇怪的 startIndex 错误。然后我注意到没有创建 App_Data 文件夹。所以我纠正了这一点,重新运行应用程序,此时数据库已创建,默认路由已正确执行,呈现主视图。

应用程序中的所有视图都基于此_Layout.cshtml,其中有一个带有一些导航链接和下拉菜单的导航栏。其中之一是链接到联系人视图的简单元素:

<a href="/Home/Contact">Contact</a>

好吧,当我单击此元素时,我得到一个 404 - 找不到文件或目录 错误。与所有其他链接一样。我试图在浏览器的地址栏中显式地编写路由,并呈现了联系人视图。当然,用户不应该知道如何编写路由。为此,我们创建了导航栏、链接等。

然后我开始 google 来解决这个问题,从那时起我已经阅读了数百篇文章、帖子、页面......其中许多都提到这个问题可以简单地解决添加web.config 上的几行代码,例如 this one。我试过。没用。

因此,在 IIS 7 中,配置如下:

  • 应用程序池:框架:v4.0.30319,托管管道:集成,身份:ApplicationPoolApplication
  • .NET 授权规则:允许所有用户
  • .NET 信任级别:完全(内部)
  • 应用程序设置:ClientValidationEnabled:true,UnobtrusiveJavaScriptEnabled:true,webpages:Enabled:false,webpages:Version: 3.0.0.0
  • 连接字符串:DbControleDeAcesso:数据源=|DataDirectory|\ControleDeAcesso.sdf; DbOrcamento:数据源=|数据目录|\Orcamento.sdf
  • 身份验证:匿名身份验证:已禁用,ASP.NET 模拟:已禁用,表单身份验证:已禁用,Windows 身份验证 em>:已启用
  • 处理程序映射
    • ExtensiolessUrlHandler-Integrated-4.0
      • 路径:*。
      • 状态:启用
      • 路径类型:未指定
      • 处理程序:System.Web.Handlers.TransferRequestHandler
      • 条目类型:本地
    • ExtensiolessUrlHandler-ISAP-4.0_32bit/64bit
      • 路径:*。
      • 状态:启用
      • 路径类型:未指定
      • 处理程序:IsapiModule
      • 条目类型:继承
  • 模块
    • UrlRoutingModule-4.0
      • 代码:System.Web.RoutingModule
      • 模块类型:托管
      • 条目类型:本地

下面我介绍我的 web.config 文件:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <appSettings>
    <add key="webpages:Version" value="3.0.0.0" />
    <add key="webpages:Enabled" value="false" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
  </appSettings>
  <system.web>
    <compilation defaultLanguage="vb" targetFramework="4.5.1" />
    <httpRuntime targetFramework="4.5.1" />
  </system.web>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
    <dependentAssembly>
      <assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" />
      <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
    </dependentAssembly>
    <dependentAssembly>
      <assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35" />
      <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0" />
    </dependentAssembly>
    <dependentAssembly>
      <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
      <bindingRedirect oldVersion="1.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" />
    </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.2.2.0" newVersion="5.2.2.0" />
    </dependentAssembly>
  </assemblyBinding>
  </runtime>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlCeConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="System.Data.SqlServerCe.4.0" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlServerCe.4.0" type="System.Data.Entity.SqlServerCompact.SqlCeProviderServices, EntityFramework.SqlServerCompact" />
    </providers>
  </entityFramework>
  <system.data>
     <DbProviderFactories>
       <remove invariant="System.Data.SqlServerCe.4.0" />
       <add name="Microsoft SQL Server Compact Data Provider 4.0" invariant="System.Data.SqlServerCe.4.0" description=".NET Framework Data Provider for Microsoft SQL Server Compact" type="System.Data.SqlServerCe.SqlCeProviderFactory, System.Data.SqlServerCe, Version=4.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" />
     </DbProviderFactories>
  </system.data>
  <connectionStrings>
      <remove name="LocalSqlServer" />
      <add name="DbControleDeAcesso" providerName="System.Data.SqlServerCe.4.0" connectionString="Data Source=|DataDirectory|\ControleDeAcesso.sdf" />
      <add name="DbOrcamento" providerName="System.Data.SqlServerCe.4.0" connectionString="Data Source=|DataDirectory|\Orcamento.sdf" />
  </connectionStrings>
  <system.webServer>
     <validation validateIntegratedModeConfiguration="false" />
     <security>
        <authentication>
            <anonymousAuthentication enabled="false" />
            <windowsAuthentication enabled="true" />
        </authentication>
    </security>
    <modules runAllManagedModulesForAllRequests="true">
        <remove name="UrlRoutingModule-4.0" />
        <add name="UrlRoutingModule-4.0" type="System.Web.Routing.UrlRoutingModule" preCondition="" />
    </modules>
    <handlers>
         <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
         <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
    </handlers>
    <defaultDocument>
        <files>
            <remove value="default.aspx" />
            <remove value="iisstart.htm" />
            <remove value="index.html" />
            <remove value="index.htm" />
            <remove value="Default.asp" />
            <remove value="Default.htm" />
        </files>
    </defaultDocument>
    <!-- <handlers>
        <add name="UrlRoutingHandler" preCondition="integratedMode" verb="*" path="UrlRouting.axd" type="System.Web.HttpForbiddenHandler, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
    </handlers> -->
  </system.webServer>
</configuration>  

我不知道它是否相关,但遵循我的 RouteConfig 类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace Orcamento.UI
{
    public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Orcamento", action = "Index", id = UrlParameter.Optional }
            );
        }
    }
}  

我相信我已经展示了有关我的问题的所有重要信息。如果我遗漏了任何内容,请告诉我。

我投入了大量时间和精力来学习如何使用 EF、jQuery 等构建 MVC 应用程序。我让它在我的开发机器上工作。我很沮丧,它不适用于生产环境。

我真的希望我能在这里得到一些有价值的帮助。

最好的问候。

保罗·里卡多·费雷拉

【问题讨论】:

  • 你怎么加了一个虚拟目录?您没有在站点文件夹中添加网站吗?
  • 嗨,@SHammelburg!我在服务器上创建了文件夹。将网站发布到该文件夹​​,然后在 IIS 中添加一个针对该文件夹的虚拟目录。感谢您的宝贵时间。
  • 那么您将虚拟目录添加到哪个网站?当您在 IIS 中时,您应该添加一个网站并将其链接到服务器上的文件夹。
  • 嗨,@SHammelburg!这正是我所做的。感谢您的宝贵时间。

标签: iis-7 asp.net-mvc-5


【解决方案1】:

@paulo Ricardo Ferreira 首先检查应用程序池是否设置为 v4.0,如果不是,则选择它。然后尝试使用 cmd 重新注册您的 4.0 版本。如图

点击进入它将开始安装,然后重新启动系统,然后单击 iis 中的路由,这将类似于顶部的 //administrator,然后转到 isapi 和 CGI​​ Restrictions 选项卡。允许所有路径,如图所示以下。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-20
    • 2021-01-31
    • 2017-02-23
    相关资源
    最近更新 更多