5

When I debug/run, using IIS Express, and browse to http://localhost:1234/People, IIS Express tries to browse the People directory instead of executing the People route, and I get a 403.14 HTTP error. So I disabled the StaticFile handler in the Web.config and refreshed. Now I get a 404.4 HTTP error:

404

I know that the route works because if I rename the RoutePrefix, e.g. PeopleTest, then the route is executed and I get the response I expect.

How can I convince IIS/Express to prefer MVC routes over static files/directories?

I am using attribute routing; the relevant code is below:

Web.config

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

    <handlers>
        <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
        <remove name="OPTIONSVerbHandler" />
        <remove name="TRACEVerbHandler" />
        <remove name="StaticFile"/>
        <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
    </handlers>
</system.webServer>

Global.asax

GlobalConfiguration.Configure(WebApiConfig.Register);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
BundleConfig.RegisterBundles(BundleTable.Bundles);
AutofacConfig.Configure();

Startup\WebApiConfig

namespace MyApi.Startup {
    public static class WebApiConfig {
        public static void Register(HttpConfiguration config) {
            config.MapHttpAttributeRoutes();
        }
    }
}

People\PeopleController

namespace MyApi.People {
    [RoutePrefix("People")]
    public partial class PagesController : BaseController {
        [Route]
        [HttpGet]
        [ResponseType(typeof(IEnumerable<Person>))]
        public IHttpActionResult Get() { ... }
    }
}

Note that since I'm using attribute routing, I am using a non-standard folder structure. E.g. I don't have the Controllers/Models/Views folders, instead I have root folders for each business area (e.g. ~\People contains the controllers/models/etc. for the "People" business area).

What I've Tried

  1. Setting RAMMFAR.
  2. Removing and re-adding ExtensionlessUrlHandler-Integrated-4.0.
Josh M.
  • 26,437
  • 24
  • 119
  • 200
  • I think you're trying to use this "People Structure" as an [Area](http://www.c-sharpcorner.com/UploadFile/4b0136/getting-started-with-area-in-mvc-5/). I'm afraid this structure you proposed will not work. – Leonel Sanches da Silva Dec 16 '15 at 16:20
  • I'm not using areas or standard MVC routing, I'm using Attribute Routing: http://blogs.msdn.com/b/webdev/archive/2013/10/17/attribute-routing-in-asp-net-mvc-5.aspx – Josh M. Dec 16 '15 at 16:39

1 Answers1

5

Fixed by adding setting RouteExistingFiles = true:

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.RouteExistingFiles = true;
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    }
}

So that ASP.NET routing will handle all routes: https://msdn.microsoft.com/en-us/library/system.web.routing.routecollection.routeexistingfiles(v=vs.110).aspx

Josh M.
  • 26,437
  • 24
  • 119
  • 200