Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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. 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.

However when I specify that I want to go to a specific controller (e.g. 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.)

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. The Default website on the remote machines IIS also already has a classic.net app running successfully.

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)

Or

2 - Something in IIS is incorrectly configured.

Any advice is welcome as a few hours of goggling has brought up no answers and I have exhausted my knowledge of IIS

Edit

Have figured it out and marked it as an answer below.

like image 572
IanSoc Avatar asked Feb 13 '23 19:02

IanSoc


2 Answers

runAllManagedModulesForAllRequests should not be used for such things.
See here for some details on that.

Most probably, your site is only missing a single module called UrlRoutingModule. During installation, MVC nugets installs it into project. 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. Since in MVC usually there's no such thing, the MVC needs this module to perform the routing properly.

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. If not, try reinstalling MVC suite. or add it manually to webconfig like it was presented in the article above.

If the module is present, check how exactly is the Site structured.

On IIS there's a slight distinction between "site" and "application". 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:

<?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.

like image 189
quetzalcoatl Avatar answered May 25 '23 16:05

quetzalcoatl


I have finally got it by reading Elian Ebbings anwser in How to host MVC application in IIS 7.0?

Turns out all that I needed to do was change the web config so that the modules section has the runAllManagedModulesForAllRequests set to true. e.g.

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>
like image 24
IanSoc Avatar answered May 25 '23 17:05

IanSoc