简体   繁体   English

IIS配置 - ASP.NET MVC返回所有请求的默认文档

[英]IIS configuration - ASP.NET MVC returns default document on all requests

Ok so I have an MVC Web application built in VS 2013. I have been able to host this application successfully through IIS on my local machine with no problems. 好吧,我有一个在VS 2013中构建的MVC Web应用程序。我已经能够通过本地计算机上的IIS成功托管此应用程序,没有任何问题。 However I now need to host on a remote machine. 但是我现在需要在远程机器上托管。 I have followed the same steps as I did on my local machine but I keep getting errors. 我遵循了与在本地计算机上执行的相同步骤,但我一直遇到错误。

When I try to browse the web application i get an error HTTP Error 403.14 - Forbidden The Web server is configured to not list the contents of this directory. 当我尝试浏览Web应用程序时出现错误HTTP错误403.14 - 禁止Web服务器配置为不列出此目录的内容。 However I have created a default document as the details of this error tell me to and now every time I try to access the application (localhost/MyWebApp) it will just go to that default.html. 但是我创建了一个默认文档,因为此错误的详细信息告诉我,现在每次我尝试访问应用程序(localhost / MyWebApp)时,它都会转到default.html。

However when I specify that I want to go to a specific controller (eg localhost/MyWebApp/Home) I get 404 error (HTTP Error 404.0 - Not Found The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.) 但是,当我指定要转到特定控制器(例如localhost / MyWebApp / Home)时,我收到404错误(HTTP错误404.0 - 未找到您要查找的资源已被删除,其名称已更改,或暂时不可用。)

The machine I am currently trying to host on does not have visual studio installed for various reasons, but I have installed ASP.NET MVC 3 and .NET 4.5 Framework on it. 我目前试图托管的机器由于各种原因没有安装Visual Studio,但我已经安装了ASP.NET MVC 3和.NET 4.5 Framework。 The Default website on the remote machines IIS also already has a classic.net app running successfully. 远程计算机IIS上的默认网站也已经成功运行了classic.net应用程序。

My thoughts are that: 我的想法是:

1 - I am missing some pre-requisite needed to host an asp.net mvc application (possibly something that is installed with visual studio) 1 - 我缺少托管asp.net mvc应用程序所需的一些先决条件(可能是与visual studio一起安装的东西)

Or 要么

2 - Something in IIS is incorrectly configured. 2 - IIS中的某些内容配置不正确。

Any advice is welcome as a few hours of goggling has brought up no answers and I have exhausted my knowledge of IIS 任何建议都是受欢迎的,因为几个小时的goggling没有提出任何答案,我已经用尽了我对IIS的了解

Edit 编辑

Have figured it out and marked it as an answer below. 已经弄清楚并将其标记为下面的答案。

I have finally got it by reading Elian Ebbings anwser in How to host MVC application in IIS 7.0? 我终于通过阅读如何在IIS 7.0中托管MVC应用程序的 Elian Ebbings anwser获得了它

Turns out all that I needed to do was change the web config so that the modules section has the runAllManagedModulesForAllRequests set to true. 事实证明,我需要做的就是更改Web配置,以便模块部分将runAllManagedModulesForAllRequests设置为true。 eg 例如

So my web config changed 所以我的网络配置改变了

From: 从:

<system.webServer>
    <modules>
      <remove name="FormsAuthenticationModule" />
    </modules>
</system.webServer>

To: 至:

<system.webServer>
    <modules runAllManagedModulesForAllRequests="true">
      <remove name="FormsAuthenticationModule" />
    </modules>
</system.webServer>

runAllManagedModulesForAllRequests should not be used for such things. runAllManagedModulesForAllRequests不应该用于此类事情。
See here for some details on that. 有关详细信息, 请参见此处

Most probably, your site is only missing a single module called UrlRoutingModule . 最有可能的是,您的网站只缺少一个名为UrlRoutingModule模块。 During installation, MVC nugets installs it into project. 在安装过程中,MVC nugets将其安装到项目中。 There were some bugs about that, but has been fixed . 有一些错误,但已得到修复

By default, ASP selects content handlers by the extension, like ASPX or JPG present in the URL. 默认情况下,ASP通过扩展名选择内容处理程序,如URL中的ASPX或JPG。 Since in MVC usually there's no such thing, the MVC needs this module to perform the routing properly. 由于在MVC中通常没有这样的东西,MVC需要这个模块来正确地执行路由。

The problem is, the routing seems to be not working in your case. 问题是,路由似乎不适用于您的情况。

First, be sure that this module is present. 首先,确保该模块存在。 Open IIS Management console, go to your page, open Modules section and check if the UrlRoutingModule is listed in the modules. 打开IIS管理控制台,转到您的页面,打开“模块”部分,检查模块中是否列出了UrlRoutingModule。 If not, try reinstalling MVC suite. 如果没有,请尝试重新安装MVC套件。 or add it manually to webconfig like it was presented in the article above. 或者手动将其添加到webconfig,就像上面的文章中所介绍的那样。

If the module is present, check how exactly is the Site structured. 如果模块存在,请检查网站的结构。

On IIS there's a slight distinction between "site" and "application". 在IIS上,“站点”和“应用程序”之间略有区别。 You might deploy your project either as a whole Site, or as an Application contained within some site. 您可以将项目部署为整个站点,也可以部署为某个站点中的应用程序。

There is a possibility that you are deploying the project as an application and that the module is "turned on" in that application's scope but not on the whole site. 您可能将项目部署为应用程序,并且该模块在该应用程序的范围内“打开”,但不在整个站点上。 If this is the case, you can add simple extra webconfig at the root of the actual Site: 如果是这种情况,您可以在实际站点的根目录添加简单的额外webconfig:

<?xml version="1.0" encoding="UTF-8" ?>
<configuration>
  <system.webServer>
    <modules>
      <remove name="UrlRoutingModule-4.0" />
      <add name="UrlRoutingModule-4.0" type="System.Web.Routing.UrlRoutingModule" preCondition="" />
    </modules>
  </system.webServer>
</configuration>

This will enable MVC routing on the whole site and will allow the http://example.com/myapplication/ to be correctly routed despite not having any "default document" set and not having any file extension in the URL. 这将在整个站点上启用MVC路由,并允许http://example.com/myapplication/正确路由,尽管没有设置任何“默认文档”且URL中没有任何文件扩展名。

You can't use links like "localhost/MyWebApp/Views/Home/Index.cshtml" in MVC app. 您不能在MVC应用程序中使用“localhost / MyWebApp / Views / Home / Index.cshtml”之类的链接。 Try "localhost/Home/". 试试“localhost / Home /”。 And check if there are any sites wich were started on 80th port. 并检查是否有任何网站在80号端口启动。 If MyWebApp is only app which works at 80th port please check was this port added as security rule in remote computer firewall. 如果MyWebApp只是在第80个端口工作的应用程序,请检查此端口是否已添加为远程计算机防火墙中的安全规则。

ASP MVC 4 (run IIS and edit code same time): ASP MVC 4(同时运行IIS和编辑代码):

Editing a .cs file while IIS is running sometimes causes problems, but it is possible to run IIS and edit the code. 在IIS运行时编辑.cs文件有时会导致问题,但可以运行IIS并编辑代码。

Just right click on a cshtml file and then click "view in browser". 只需右键单击cshtml文件,然后单击“在浏览器中查看”。 It will run IIS and then you can edit any .cs file and see the output of any 'view' at the same time. 它将运行IIS,然后您可以编辑任何.cs文件并同时查看任何“视图”的输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM